camera-samples
camera-samples copied to clipboard
Camera is closed exception on takePicture()
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!
thank you for reporting the issue! To get on this issue, may you narrow down the scope, if you could have a small app to demo it, that is great; os version, device model(s). Unless this consistently happens on most devices(Pixel device is easy to get by in our org).
Hey @ggfan , Sorry for replying late, actually this is happening in our production application. So basically the app doesnt crash so I dont really have any clear logs. I am just able to get some message which says "camera is closed" in the onError callback.
Brand : Nokia Android version : 10 Model : Nokia 6.1 Plus
We are also getting this error, reported on Samsung Galaxy A52 5G, I have logs for the same. We are using CameraX stable version. The user is taking pictures continuously and it occurs in between. Please find the logs below. Thanks Ram
2021-09-01 18:22:16.79 +0530 IST: Image file created: /storage/emulated/0/Android/data/com.insigma.iredccswb/files/Pictures/IMG_20210901_085216789.jpg 2021-09-01 18:22:19.606 +0530 IST: Image quality is good 2021-09-01 18:22:47.489 +0530 IST: Image file created: /storage/emulated/0/Android/data/com.insigma.iredccswb/files/Pictures/IMG_20210901_085247487.jpg 2021-09-01 18:22:50.307 +0530 IST: Image quality is good 2021-09-01 18:22:57.275 +0530 IST: Image file created: /storage/emulated/0/Android/data/com.insigma.iredccswb/files/Pictures/IMG_20210901_085257273.jpg
//LOTS OF PICTURES TAKEN AS IN THE ABOVE LOGS //NOW ERROR OCCURED 2021-09-01 18:23:45.273 +0530 IST error CameraXActivity androidx.camera.core.ImageCaptureException: Camera is closed.\n at androidx.camera.core.ImageCapture$ImageCaptureRequest.lambda$notifyCallbackError$1$ImageCapture$ImageCaptureRequest(ImageCapture.java:2409)\n at androidx.camera.core.-$$Lambda$ImageCapture$ImageCaptureRequest$KlqAxzwB-08wcOFrjThjf8ncF2g.run(Unknown Source:8)\n at android.os.Handler.handleCallback(Handler.java:883)\n at android.os.Handler.dispatchMessage(Handler.java:100)\n at android.os.Looper.loop(Looper.java:237)\n at android.app.ActivityThread.main(ActivityThread.java:8167)\n at java.lang.reflect.Method.invoke(Native Method)\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)\nCaused by: androidx.camera.core.CameraClosedException: Camera is closed.\n at androidx.camera.core.ImageCapture.abortImageCaptureRequests(ImageCapture.java:885)\n at androidx.camera.core.ImageCapture.onStateDetached(ImageCapture.java:881)\n at androidx.camera.camera2.internal.Camera2CameraImpl.notifyStateDetachedToUseCases(Camera2CameraImpl.java:732)\n at androidx.camera.camera2.internal.Camera2CameraImpl.detachUseCases(Camera2CameraImpl.java:772)\n at androidx.camera.core.internal.CameraUseCaseAdapter.detachUseCases(CameraUseCaseAdapter.java:292)\n at androidx.camera.lifecycle.LifecycleCamera.onStop(LifecycleCamera.java:93)\n at androidx.camera.lifecycle.LifecycleCamera.suspend(LifecycleCamera.java:119)\n at androidx.camera.lifecycle.LifecycleCameraRepository.suspendUseCases(LifecycleCameraRepository.java:433)\n at androidx.camera.lifecycle.LifecycleCameraRepository.setInactive(LifecycleCameraRepository.java:387)\n at androidx.camera.lifecycle.LifecycleCameraRepository$LifecycleCameraRepositoryObserver.onStop(LifecycleCameraRepository.java:504)\n at java.lang.reflect.Method.invoke(Native Method)\n at androidx.lifecycle.ClassesInfoCache$MethodReference.invokeCallback(ClassesInfoCache.java:219)\n at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeMethodsForEvent(ClassesInfoCache.java:194)\n at androidx.lifecycle.ClassesInfoCache$CallbackInfo.invokeCallbacks(ClassesInfoCache.java:185)\n at androidx.lifecycle.ReflectiveGenericLifecycleObserver.onStateChanged(ReflectiveGenericLifecycleObserver.java:37)\n at androidx.lifecycle.LifecycleRegistry$ObserverWithState.dispatchEvent(LifecycleRegistry.java:354)\n at androidx.lifecycle.LifecycleRegistry.backwardPass(LifecycleRegistry.java:284)\n at androidx.lifecycle.LifecycleRegistry.sync(LifecycleRegistry.java:302)\n at androidx.lifecycle.LifecycleRegistry.moveToState(LifecycleRegistry.java:148)\n at androidx.lifecycle.LifecycleRegistry.handleLifecycleEvent(LifecycleRegistry.java:134)\n at androidx.lifecycle.ReportFragment.dispatch(ReportFragment.java:68)\n at androidx.lifecycle.ReportFragment$LifecycleCallbacks.onActivityPreStopped(ReportFragment.java:210)\n at android.app.Activity.dispatchActivityPreStopped(Activity.java:1345)\n at android.app.Activity.performStop(Activity.java:8180)\n at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:5117)\n at android.app.ActivityThread.handleSleeping(ActivityThread.java:5254)\n at android.app.ActivityThread.access$2700(ActivityThread.java:274)\n at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2200)\n at android.os.Handler.dispatchMessage(Handler.java:107)\n ... 5 more\n 2021-09-01 18:25:50.077 +0530 IST trace IRCamera BugfenderUtils.java:20 15197_e27e33e8-a04d-5a0f-9b42-8992ce6b4ea8 3.8.6 45 en 10 2 482 Image file created: /storage/emulated/0/Android/data/com.insigma.iredccswb/files/Pictures/IMG_20210901_085550075.jpg
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()
It's a emulator issue.
Change emulators camera setting Emulated
instead of VirtualScene
, will fix the issue
I am afraid it's not just emulator issue. I can see the happening for users in production with device Samsung Galaxy S22 Ultra. This is definitely not just an emulator issue.
same here
androidx.camera.core.ImageCaptureException: Camera is closed.
at androidx.camera.core.ImageCapture$ImageCaptureRequest.lambda$notifyCallbackError$1(SourceFile:2312)
at androidx.camera.core.ImageCapture$ImageCaptureRequest.$r8$lambda$KlqAxzwB-08wcOFrjThjf8ncF2g
at androidx.camera.core.ImageCapture$ImageCaptureRequest$$InternalSyntheticLambda$1$144ba365f39cf07dfc4635573e3d066c10b3daa8cdfe57668b325d6de4cc2219$0.run
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:923)
I can just add the scenario which was the cause in my particular case and how I fixed that.
For me this issue happened when the app was sent to the background after triggering a picture but before picture was saved. In this case camera fired ImageCaptureException. I simply try catch this exception and return null instead of photoFile. No more ImageCaptureException in production since then.
Hope it will help to the others.
Getting same exception in Device Manufacturer- OPPO OS Version- 12 Device Name- OP4EF3L1
imageCapture1.takePicture( outputOptions, ContextCompat.getMainExecutor(requireContext()), object : ImageCapture.OnImageSavedCallback { override fun onError(exc: ImageCaptureException) { "Camera is closed" }