why_rookie
why_rookie

Reputation: 78

I get an error when I use the code "imageCapture.takePicture" of CameraX

I get the error

"java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List androidx.camera.core.SessionConfig$Builder.getSingleCameraCaptureCallbacks()' on a null object reference"

when I use the code below:

 val file = File(externalMediaDirs.first(),"${System.currentTimeMillis()}.jpg")
        imageCapture.takePicture(file,executor,object : ImageCapture.OnImageSavedListener{
            override fun onImageSaved(file: File) {
                val msg = "success: ${file.absolutePath}"
                Log.d("CameraXApp", msg)
                viewFinder.post {
                    Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                }
            }

            override fun onError(
                imageCaptureError: ImageCapture.ImageCaptureError,
                message: String,
                cause: Throwable?
            ) {
                val msg = "failed: $message"
                Log.e("CameraXApp", msg, cause)
                viewFinder.post {
                    Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                }
            }

        })

Is there a callback that I haven't setted?

Upvotes: 1

Views: 5532

Answers (1)

Lahiru Muthumal
Lahiru Muthumal

Reputation: 36

you can use the below code to initialize imageCapture

imageCapture=ImageCapture.Builder().build()

cameraProvider.bindToLifecycle( this, cameraSelector, preview,imageCapture)

Upvotes: 2

Related Questions