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)
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With