Search code examples
flutterdartandroid-camera2

Flutter: Camera disconnect exception when other camera apps are opened


I'm currently using the flutter camera package to record and save videos, but I've noticed that the camera preview runs into an exception when the device's camera is opened on another app or when face unlock is used on the lock screen.

I've tried using didChangeAppLifecycleState to possibly fetch or reinitialize the camera, but there's no success yet.

      await model.fetchCameras();
      if (model.cameras.isNotEmpty) {
        await model.onNewCameraSelected(model.cameras[model.cameraIndex], true);

      }
    }

This issue is currently opened here and here, but haven't been resolved.

I get this exception when the device's camera is opened in another app. I tried disposing the camera and reinitializing it in the sample code above, but it doesn't work.

E/CameraCaptureSession(31468): android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): cancelRequest:458: Camera device no longer alive
E/CameraCaptureSession(31468):  at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:814)
E/CameraCaptureSession(31468):  at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRequest(ICameraDeviceUserWrapper.java:95)
E/CameraCaptureSession(31468):  at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1134)
E/CameraCaptureSession(31468):  at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:526)
E/CameraCaptureSession(31468):  at android.hardware.camera2.impl.CameraCaptureSessionImpl$2.onDisconnected(CameraCaptureSessionImpl.java:737)
E/CameraCaptureSession(31468):  at android.hardware.camera2.impl.CameraDeviceImpl$7.run(CameraDeviceImpl.java:242)
E/CameraCaptureSession(31468):  at android.os.Handler.handleCallback(Handler.java:873)
E/CameraCaptureSession(31468):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/CameraCaptureSession(31468):  at android.os.Looper.loop(Looper.java:214)
E/CameraCaptureSession(31468):  at android.app.ActivityThread.main(ActivityThread.java:6981)
E/CameraCaptureSession(31468):  at java.lang.reflect.Method.invoke(Native Method)
E/CameraCaptureSession(31468):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/CameraCaptureSession(31468):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
E/CameraCaptureSession(31468): Caused by: android.os.ServiceSpecificException: cancelRequest:458: Camera device no longer alive (code 4)
E/CameraCaptureSession(31468):  at android.os.Parcel.createException(Parcel.java:1980)
E/CameraCaptureSession(31468):  at android.os.Parcel.readException(Parcel.java:1934)
E/CameraCaptureSession(31468):  at android.os.Parcel.readException(Parcel.java:1884)
E/CameraCaptureSession(31468):  at android.hardware.camera2.ICameraDeviceUser$Stub$Proxy.cancelRequest(ICameraDeviceUser.java:402)
E/CameraCaptureSession(31468):  at android.hardware.camera2.impl.ICameraDeviceUserWrapper.cancelRe

Any Ideas, on how to force the camera package to reinitialize?


Solution

  • On resume re initialise Camera

    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
      if (state == AppLifecycleState.resumed) {
        _controller != null
            ? _initializeControllerFuture = _controller.initialize()
            : null; //on pause camera is disposed, so we need to call again "issue is only for android"
      }
    }
    

    checkout https://medium.com/@navinkumar0118/take-a-picture-using-flutter-camera-a9c11d282632