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)
In my case this error was preceded by
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
inupdateRepeatingRequest
beforeoverride fun onConfigured(s: CameraCaptureSession) {
Clearing surfaces in
initializeCamera()
and removing the additional updateRepeatingRequest fixed it for me.