question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Camera is closed exception on takePicture()

See original GitHub issue

Hi, I am using camera2 in my app. Generally camera works fine for most of the users but for some users when they try capture image an exception is thrown with message “Camera is closed.” While this error occurs camera is actually actively working on UI, so not sure why this error is happening.

Code I am using to capture image is :

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.getMessage() --> Camera is closed.
                        }
                    });

Please let me know how to fix this. Thank you!

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
bkmalkoccommented, Oct 21, 2021

It’s a emulator issue. Change emulators camera setting Emulated instead of VirtualScene, will fix the issue

0reactions
etorres1974commented, Oct 15, 2021

I am also getting this errof but only for android API 31 and using Back Camera.
I tested the CameraXBasic sample on many pixels devices and different CameraX versions and always getting this log bellow. When testing on lower Android Api or Using Front camera i could not reproduce this.

2021-10-15 14:26:56.765 3115-3221/com.android.example.cameraxbasic D/CameraStateMachine: New public camera state CameraState{type=OPENING, error=StateError{code=3, cause=null}} from OPENING and StateError{code=3, cause=null} 2021-10-15 14:26:56.765 3115-3221/com.android.example.cameraxbasic D/CameraStateMachine: Publishing new public camera state CameraState{type=OPENING, error=StateError{code=3, cause=null}} 2021-10-15 14:26:56.774 3115-3221/com.android.example.cameraxbasic D/Camera2CameraImpl: {Camera@9353d8[id=0]} Resetting Capture Session 2021-10-15 14:26:56.783 3115-3221/com.android.example.cameraxbasic E/CameraCaptureSession: Session 0: Exception while stopping repeating: android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): The camera device has encountered a serious error at android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:2350) at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1277) at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:579) at androidx.camera.camera2.internal.SynchronizedCaptureSessionBaseImpl.close(SynchronizedCaptureSessionBaseImpl.java:464) at androidx.camera.camera2.internal.CaptureSession.release(CaptureSession.java:412) at androidx.camera.camera2.internal.Camera2CameraImpl.releaseSession(Camera2CameraImpl.java:517) at androidx.camera.camera2.internal.Camera2CameraImpl.resetCaptureSession(Camera2CameraImpl.java:1179) at androidx.camera.camera2.internal.Camera2CameraImpl.closeCamera(Camera2CameraImpl.java:431) at androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback.reopenCameraAfterError(Camera2CameraImpl.java:1647) at androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback.handleErrorOnOpen(Camera2CameraImpl.java:1599) at androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback.onError(Camera2CameraImpl.java:1575) at androidx.camera.camera2.internal.CameraDeviceStateCallbacks$ComboDeviceStateCallback.onError(CameraDeviceStateCallbacks.java:122) at android.hardware.camera2.impl.CameraDeviceImpl.notifyError(CameraDeviceImpl.java:1748) at android.hardware.camera2.impl.CameraDeviceImpl.$r8$lambda$KBQCqQRdhVVn7uHI9Xdha6OqnsU(Unknown Source:0) at android.hardware.camera2.impl.CameraDeviceImpl$$ExternalSyntheticLambda0.accept(Unknown Source:8) at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:281) at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:204) at com.android.internal.util.function.pooled.OmniFunction.run(OmniFunction.java:97) at androidx.camera.core.impl.utils.executor.SequentialExecutor$1.run(SequentialExecutor.java:111) at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.workOnQueue(SequentialExecutor.java:231) at androidx.camera.core.impl.utils.executor.SequentialExecutor$QueueWorker.run(SequentialExecutor.java:173) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:920) 2021-10-15 14:26:56.785 3115-3221/com.android.example.cameraxbasic D/Camera2CameraImpl: {Camera@9353d8[id=0]} Releasing session in state REOPENING 2021-10-15 14:26:56.787 3115-3221/com.android.example.cameraxbasic D/Camera2CameraImpl: {Camera@9353d8[id=0]} CameraDevice.onDisconnected()

Read more comments on GitHub >

github_iconTop Results From Across the Web

Camera is closed. In camera2 on takePicture() - Stack Overflow
In CameraX the ImageCapture use case is stable so this bug shouldn't occur even rarely, so in the cases of the error, the...
Read more >
androidx.camera.core.ImageCaptureException ... - Issue Tracker
1. click on camera and try to capture the image , it gives below error ... private fun bindCameraUseCases() { ... "error in...
Read more >
ImageCaptureException - Android Developers
An exception thrown to indicate an error has occurred during image capture or while saving the captured image. See ImageCapture.OnImageCapturedCallback and ...
Read more >
Accessing the Camera and Stored Media - CodePath Cliffnotes
We need to define the getPhotoFileUri() function: Java; Kotlin. // Returns the File for a photo stored on disk given the fileName public...
Read more >
ImageCapture.java - android Git repositories
It provides takePicture() functions to take ... An error indicating the request cannot be done due to camera is closed.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found