Search code examples
androidandroid-cameraandroid-camerax

Problem with binding camerax on android api 26


I have such error:

Use case binding failed No supported surface combination is found for camera device - Id : 1.  May be attempting to bind too many use cases. Existing surfaces: [] New configs: [androidx.camera.core.impl.PreviewConfig@43936ec, androidx.camera.core.impl.ImageCaptureConfig@1a07ab5, androidx.camera.video.impl.VideoCaptureConfig@3b5954a]

I think it is connected with this code scope:

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


        cameraProviderFuture.addListener({
            // Used to bind the lifecycle of cameras to the lifecycle owner
            val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()

            // Unbind use cases before rebinding
            cameraProvider.unbindAll()

            // Preview
            val preview = Preview.Builder()
                .build()
                .also {
                    it.setSurfaceProvider(binding.previewView.surfaceProvider)
                }

            // Select back camera as a default
            val cameraSelector = if (intent.extras?.getBoolean("default_cam") == true) CameraSelector.DEFAULT_BACK_CAMERA else CameraSelector.DEFAULT_FRONT_CAMERA

            try {
                // Bind use cases to camera
                cameraProvider.bindToLifecycle(
                    this, cameraSelector, preview, imageCapture, videoCapture)

            } catch (exc: Exception) {
                println("Use case binding failed ${exc.localizedMessage}")
            }

        }, ContextCompat.getMainExecutor(this))

        val recorder = Recorder.Builder()
            .setQualitySelector(QualitySelector.from(Quality.LOWEST))
            .build()
        videoCapture = VideoCapture.withOutput(recorder)

        imageCapture = ImageCapture.Builder().apply {
            setTargetRotation(Surface.ROTATION_0)
            setTargetResolution(Size(720, 1280))
        }.build()
    }

As you can see I unbinded all use cases before new binding. This code is located in my activity, and the strange issue is that I don't see any preview on android 7,7.1,7.2 but I see it on android 8+. I removed android:hardwareAccelerated=true from my AndroidManifest.xml but it didn't help me :(

UPDATE

One of possible solutions is posted here and is connected with binding all use cases simultaneously. So with this line:

cameraProvider.bindToLifecycle(this, cameraSelector, preview)

instead of this:

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

I can see the preview, but it is not ok for me to see only preview without use cases


Solution

  • Right now the only solution which I had managed to find it to handle capture mode and set only one user during binding:

    cameraProvider.bindToLifecycle(
                        this,
                        cameraSelector,
                        preview,
                        if (intent.extras?.getBoolean("is_video") == true) videoCapture else imageCapture
                    )
    

    I don't sure whether it is ok, but I can't any more accurate solution :(