ImageCaptureException : Camera is closed. In camera2 on takePicture()

Viewed 458

I am facing this issue, it occurs rarely but once it occurs user cannot go forward as there is no workaround that I am able to do.

While the user clicks on capture the camera is working in the background, but still i am getting camera is closed error.

Device info :
Brand : Nokia
Android version : 10
Model : Nokia 6.1 Plus

This is the code I am using to capture image

imageCapture.takePicture(outputFileOptions, ContextCompat.getMainExecutor(getContext()),
                    new ImageCapture.OnImageSavedCallback() {
                        @Override
                        public void onImageSaved(ImageCapture.OutputFileResults outputFileResults) {

                            imagePath = FileProvider
                                    .getUriForFile(getContext(), getContext().getPackageName() + ".provider", imageFile);
                            checkImageOrientation();
                        }

                        @Override
                        public void onError(ImageCaptureException error) {
                         // Error is : **Camera is closed.**
                        }
                    });
1 Answers

In CameraX the ImageCapture use case is stable so this bug shouldn't occur even rarely, so in the cases of the error, the camera is closed due to either a problem in your logic of when exactly you are calling imageCapture.takePicture(), or for example when you take a picture and the app pauses before the image is saved, it will throw the error you mentioned.

Related