7

I am using camerax to capture images in my android app. Everything is working fine for me but some users are reporting black preview screen when using camerax activity.

But when users opens the app from recents app, the preview seems to work. So, I think the issue might be with the lifecycle binding.

I am using

implementation "androidx.camera:camera-camera2:1.0.0-beta08"

Here is my code

    <androidx.camera.view.PreviewView
        android:id="@+id/viewFinder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />


     private fun startCamera() {
        val cameraProviderFuture = ProcessCameraProvider.getInstance(this)

        cameraProviderFuture.addListener({
            cameraProvider = cameraProviderFuture.get()

            val preview = Preview.Builder()
                    .build()
                    .also {
                        it.setSurfaceProvider(viewFinder.createSurfaceProvider())
                    }

            imageCapture = ImageCapture.Builder()
                    .build()

            val cameraSelector = CameraSelector.Builder()
                    .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                    .build()
            
            try {
                cameraProvider.unbindAll()
                cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture)

            } catch (exc: Exception) {
                Log.e(TAG, "Use case binding failed", exc)
            }

        }, ContextCompat.getMainExecutor(this))
    }


     private fun takePhoto() {
        val imageCapture = imageCapture ?: return

        val photoFile = File(
                getExternalFilesDir("scantmp"),
                SimpleDateFormat(FILENAME_FORMAT, Locale.US
                ).format(System.currentTimeMillis()) + ".png")

        val outputOptions = ImageCapture
                .OutputFileOptions
                .Builder(photoFile)
                .build()

        imageCapture.takePicture(
                outputOptions, ContextCompat.getMainExecutor(this), object : ImageCapture.OnImageSavedCallback {
            override fun onError(exc: ImageCaptureException) {
                Log.e(TAG, "Photo capture failed: ${exc.message}", exc)
            }

            override fun onImageSaved(output: ImageCapture.OutputFileResults) {
                savedImageUri = Uri.fromFile(photoFile)
                flash.visibility = View.GONE
                closeCamera.visibility = View.GONE
                takeAgain.visibility = View.VISIBLE
                saveImage.visibility = View.VISIBLE
                imgCapture.visibility = View.INVISIBLE
                imageCaptured.setImageURI(Uri.fromFile(photoFile))
                imageCaptured.visibility = View.VISIBLE
                viewFinder.visibility = View.GONE

            }
        })
    }

2
  • Your setup looks fine. Could you post the code you're using to request the necessary permission (e.g. Camera permission)? Sep 16, 2020 at 2:21
  • Camera permission is already granted on the min activity. So, there is no issue with the permission. Sep 16, 2020 at 3:57

1 Answer 1

2

The above problem is solved in 1.0.0-beta11 update. Try updating your library to latest version to solve this problem.

1
  • that fails when I set camerax_version = '1.1.0'
    – Larry Lo
    Jan 3 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.