ImageCaptureException: Not bound to a valid Camera

754 Views Asked by At

I tried to follow the instructions from the google codelab: https://developer.android.com/codelabs/camerax-getting-started#0

Currently, I am getting the following exception message when trying to capture a picture:

androidx.camera.core.ImageCaptureException: Not bound to a valid Camera

The version of camerax is 1.2.0-alpha01. The preview is working. I can see the camera.

I appreciate any help. Thank you in advance.

My method takePhoto:

    @SuppressLint("RestrictedApi")
private void takePhoto() {
    ImageCapture imageCapture = new ImageCapture.Builder()
            .setTargetRotation(Surface.ROTATION_0)
            .build();

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS", Locale.US);
    String name = simpleDateFormat.format(System.currentTimeMillis());

    ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, name);
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg");

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
        contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/CameraX-Image");
    }

    ImageCapture.OutputFileOptions outputFileOptions = new ImageCapture.OutputFileOptions.Builder(getContentResolver(),
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            contentValues)
            .build();
    
    imageCapture.takePicture(outputFileOptions,
            ContextCompat.getMainExecutor(this),
            new ImageCapture.OnImageSavedCallback() {
                @Override
                public void onImageSaved(@NonNull ImageCapture.OutputFileResults outputFileResults) {
                    Uri savedUri = outputFileResults.getSavedUri();
                    String msg = "Photo capture succeeded: $savedUri";
                    Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
                    Log.d(TAG, msg);
                }

                @Override
                public void onError(@NonNull ImageCaptureException exception) {
                    Log.e(TAG, "${exception.message}", exception);
                }
            });
}

My method startCamera:

    private void startCamera() {
    ListenableFuture<ProcessCameraProvider> providerListenableFuture = ProcessCameraProvider.getInstance(this);

    providerListenableFuture.addListener(() -> {
        try {
            ImageCapture imageCapture = new ImageCapture.Builder()
                    .setTargetRotation(Surface.ROTATION_0)
                    .build();

            ProcessCameraProvider cameraProvider = providerListenableFuture.get();
            int rotation = viewBinding.viewFinder.getDisplay().getRotation();

            Preview preview = new Preview.Builder().setTargetRotation(rotation).build();
            preview.setSurfaceProvider(viewBinding.viewFinder.getSurfaceProvider());
            CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA;

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

        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }, ContextCompat.getMainExecutor(this));
}
0

There are 0 best solutions below