androidandroid-cameralifecycleandroid-camerax

CameraX black screen preview on some devices


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

            }
        })
    }


Solution

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