camerax "Not bound to a valid Camera" for imageCapture use case on Android 11
Asked Answered
P

3

5

I'm consistently getting the error

Not bound to a valid Camera

when attempting to implement the imageCapture use case. I'm implementing the feature in Java following the camerax tutorial here.

The preview use case binds correctly and I can access the camera when I launch the activity, but when I tap on the "take photo" button, I get the error below:

Photo capture failed: ${exception.message}
    androidx.camera.core.ImageCaptureException: Not bound to a valid Camera [ImageCapture:androidx.camera.core.ImageCapture-9aad1b57-8fb6-4a6e-b528-9347343369b6]
        at androidx.camera.core.ImageCapture.lambda$sendImageCaptureRequest$6$ImageCapture(ImageCapture.java:825)
        at androidx.camera.core.-$$Lambda$ImageCapture$2B91NcXQyr59NDscigcxsZb94mc.run(Unknown Source:4)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Anyone facing a similar issue? Any pointers?

Penalize answered 17/11, 2020 at 6:35 Comment(2)
Have a look at this answer - https://mcmap.net/q/722910/-quot-not-bound-to-a-valid-camera-quot-camerax-errorGusella
Thanks @Gusella I got it to work by including the bind statement in the takePhoto function. This is not explicitly mentioned in the tutorial. So for anyone following the tutorial, in addition to making the bind statement in the startCamera function, please include a similar bind call in the takePhoto function, attaching the imageCapture use case.Penalize
R
5
cameraProvider.bindToLifecycle(
   this, cameraSelector, preview, imageCapture)
Ridgepole answered 27/12, 2020 at 12:50 Comment(1)
Please add additional details to your answerFlagman
K
3

To fix this problem you have to bind the collections of the UseCase to lifeCycleOwner.

What is the useCase:

A UseCase provides functionality to map the set of arguments in a use case to arguments that are usable by a camera. UseCase also will communicate of the active / inactive state to the Camera.

First you must have the dependencies below in your build.gradel :

dependencies {
        /*
          rest of your implementation
        */
        // CameraX core library using the camera2 implementation
        def camerax_version = "1.1.0-alpha03"
        // The following line is optional, as the core library is included indirectly by camera-camera2
        implementation "androidx.camera:camera-core:${camerax_version}"
        implementation "androidx.camera:camera-camera2:${camerax_version}"
        // If you want to additionally use the CameraX Lifecycle library
        implementation "androidx.camera:camera-lifecycle:${camerax_version}"
  }

And now you can bind it by using :

public androidx.camera.core.Camera bindToLifecycle(@NonNull @NotNull androidx.lifecycle.LifecycleOwner lifecycleOwner,
                                                   @NonNull @NotNull androidx.camera.core.CameraSelector cameraSelector,
                                                   @NonNull @NotNull androidx.camera.core.UseCase... useCases)

For exmaple i have this code in my MainActivity :

For java :

UseCase imageCapture = new ImageCapture.Builder()
            .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
            .build();

ListenableFuture<ProcessCameraProvider> providerListenableFuture = ProcessCameraProvider.getInstance(this);
ProcessCameraProvider processCameraProvider = providerListenableFuture.get();
processCameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, imageCapture);

And for Kotlin :

val outputFileOptions = ImageCapture.OutputFileOptions.Builder(file).build()
val imageCapture = ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
                .build()

val cameraProviderFuture: ListenableFuture<ProcessCameraProvider> = ProcessCameraProvider.getInstance(this)
val processCameraProvider = cameraProviderFuture.get()
processCameraProvider.bindToLifecycle(this, CameraSelector.DEFAULT_BACK_CAMERA, imageCapture)

For more details consult the implementation of the CameraX on the official documentation

Kant answered 7/4, 2021 at 23:38 Comment(1)
what is processCameraProvider, all examples I see use just camera provider with an Executor and how do I use it?Antofagasta
M
1

Ensure you also bind for preview and imageCapture when doing startCamera

// Bind use cases to camera    
cameraProvider.bindToLifecycle(this, cameraSelector, imageCapture, preview)
Montmartre answered 5/8, 2021 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.