Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - CameraDevice was already closed

Tags:

I was trying so long for Camera2 api integration to my app. When i switch from portrait mode to landscape it throws exception

    java.lang.IllegalStateException: CameraDevice was already closed
at android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:1984)
at android.hardware.camera2.impl.CameraDeviceImpl.submitCaptureRequest(CameraDeviceImpl.java:825)
at android.hardware.camera2.impl.CameraDeviceImpl.setRepeatingRequest(CameraDeviceImpl.java:880)
at android.hardware.camera2.impl.CameraCaptureSessionImpl.setRepeatingRequest(CameraCaptureSessionImpl.java:231)
at com.essentialsln.memtalk.ui.fragments.Camera2Fragment$11.onConfigured(Camera2Fragment.java:795)
at java.lang.reflect.Method.invoke(Native Method)
at android.hardware.camera2.dispatch.InvokeDispatcher.dispatch(InvokeDispatcher.java:39)
at android.hardware.camera2.dispatch.HandlerDispatcher$1.run(HandlerDispatcher.java:65)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.os.HandlerThread.run(HandlerThread.java:61)
like image 982
Giorgi Asaturyan Avatar asked Aug 31 '17 21:08

Giorgi Asaturyan


2 Answers

I faced a similar problem and this helped me. If you have been referring to the Sample app, you should stop the thread in your CameraDevice.StateCallback by overriding the onClosed(CameraDevice) method:

 mCameraDevice.createCaptureSession(outputSurfaces,
                new CameraCaptureSession.StateCallback() {

                    @Override
                    public void onClosed(@NonNull CameraCaptureSession session) {
                        super.onClosed(session);
                        stopBackgroundThread();
                    } 
like image 158
Ankit Deshmukh Avatar answered Oct 20 '22 03:10

Ankit Deshmukh


In my case this error was preceded by

2020-12-15 18:31:55.703 5079-5079/com.android.example.camera2.video W/CameraDevice-JV-0: Stream configuration failed due to: createStream:1008: Camera 0: Surface already has a stream created for it (ID 0)
2020-12-15 18:31:55.704 5079-5079/com.android.example.camera2.video E/CameraCaptureSession: Session 0: Failed to create capture session; configuration failed

The problem was that I was adding surfaces to a list and adding it as a target but not clearing it. I was also setRepeatingRequest in updateRepeatingRequest before override fun onConfigured(s: CameraCaptureSession) {

Clearing surfaces in initializeCamera() and removing the additional updateRepeatingRequest fixed it for me.

like image 21
zeitgeist Avatar answered Oct 20 '22 04:10

zeitgeist