前提・実現したいこと
AndroidStudioでFotoapparatライブラリを用いてカメラから取得した画像をImageViewに表示するプログラムをKotlinで作成しています。
fotoapparat.takePicture()で得た写真データをimageViewに表示するプログラムを書いています。
エラーが発生していないのを確認し、実機を用いてアプリを起動したのですが、一瞬画面が表示された後にアプリが落ちてしまいました。
エラーメッセージでは、カメラについて初期化されていないという例外が発生しています。
上記の例外が原因でアプリが落ちてしまっているのだと考えているのですが、どう修正すれば良いのかわかりません。
ご教授頂ける方がいましたら、宜しくお願い致します。
発生している問題・エラーメッセージ
Process: com.example.cameraapp_usefotoapprat_bykotlin, PID: 11411 java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:504) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: kotlin.UninitializedPropertyAccessException: lateinit property camera has not been initialized at java.util.concurrent.FutureTask.report(FutureTask.java:123) at java.util.concurrent.FutureTask.get(FutureTask.java:193) at io.fotoapparat.result.PendingResult.await(PendingResult.kt:55) at com.example.cameraapp_usefotoapprat_bykotlin.MainActivity.onStart(MainActivity.kt:39) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391) at android.app.Activity.performStart(Activity.java:7348) at android.app.ActivityThread.handleStartActivity(ActivityThread.java:3138) at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180) at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7050) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) Caused by: java.util.concurrent.ExecutionException: kotlin.UninitializedPropertyAccessException: lateinit property camera has not been initialized at java.util.concurrent.FutureTask.report(FutureTask.java:123) at java.util.concurrent.FutureTask.get(FutureTask.java:193) at io.fotoapparat.result.PendingResult$transform$transformTask$1.call(PendingResult.kt:36) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764) Caused by: kotlin.UninitializedPropertyAccessException: lateinit property camera has not been initialized at io.fotoapparat.hardware.CameraDevice.takePhoto(CameraDevice.kt:139) at io.fotoapparat.routine.photo.TakePhotoRoutineKt$takePhoto$1.invokeSuspend(TakePhotoRoutine.kt:15) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask$DefaultImpls.run(Dispatched.kt:235) at kotlinx.coroutines.DispatchedContinuation.run(Dispatched.kt:81) at kotlinx.coroutines.EventLoopBase.processNextEvent(EventLoop.kt:123) at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:69) at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:45) at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source:1) at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:35) at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source:1) at io.fotoapparat.routine.photo.TakePhotoRoutineKt.takePhoto(TakePhotoRoutine.kt:12) at io.fotoapparat.Fotoapparat$takePicture$future$1.invoke(Fotoapparat.kt:119) at io.fotoapparat.Fotoapparat$takePicture$future$1.invoke(Fotoapparat.kt:34) at io.fotoapparat.concurrent.CameraExecutor$execute$future$1.call(CameraExecutor.kt:26) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764)
該当のソースコード
MainActivity.kt package com.example.cameraapp_usefotoapprat_bykotlin import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.ImageView import android.widget.TextView import io.fotoapparat.Fotoapparat import io.fotoapparat.parameter.ScaleType import io.fotoapparat.selector.back import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { lateinit var fotoapparat: Fotoapparat override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) fotoapparat = Fotoapparat( context = this, view = cameraview, scaleType = ScaleType.CenterCrop, lensPosition = back() ) } override fun onStart() { super.onStart() fotoapparat.start() var photoResult = fotoapparat.takePicture() var result = photoResult.toBitmap().await() var imageview :ImageView=findViewById(R.id.imageView) imageview.setImageBitmap(result.bitmap) } override fun onResume() { super.onResume() } override fun onStop() { super.onStop() fotoapparat.stop() } }
activity_main.xml <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="match_parent" /> <io.fotoapparat.view.CameraView android:id="@+id/cameraview" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0"> </io.fotoapparat.view.CameraView> </androidx.constraintlayout.widget.ConstraintLayout>
build.gradle(App) apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 29 buildToolsVersion "29.0.3" defaultConfig { applicationId "com.example.cameraapp_usefotoapprat_bykotlin" minSdkVersion 28 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.core:core-ktx:1.0.2' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'io.fotoapparat:fotoapparat:2.7.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
build.gradle(Project) // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.3.72' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.5.3' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
試したこと
補足情報(FW/ツールのバージョンなど)
使用環境:AndroidStudio
使用言語:kotlin
SDKバージョン:29(API29 Android 10.0(Q))
使用ライブラリ:Fotoapparat(https://github.com/RedApparat/Fotoapparat)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。