Search code examples
javaandroidexceptionandroid-camera2

Android camera2 API CameraManager.openCamera(...) problem while screen is locked


Please help me. I am developing a mobile application that needs to stream a video to a PC. One of the requirements is to start work with a locked phone. But every time I try to open CameraDevice it causes an exception.

android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): connectHelper:1735: Camera "0" disabled by policy
        at android.hardware.camera2.CameraManager.throwAsPublicException(CameraManager.java:1009)
        at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:544)
        at android.hardware.camera2.CameraManager.openCameraForUid(CameraManager.java:725)
        at android.hardware.camera2.CameraManager.openCamera(CameraManager.java:693)
        at com.pv.imageview.hardware.CameraAccess.startRecord(CameraAccess.java:293)
        at com.pv.imageview.LoopService.onStartCommand(LoopService.java:52)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:4320)
        at android.app.ActivityThread.access$1800(ActivityThread.java:237)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1951)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7660)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
W/System.err: Caused by: android.os.ServiceSpecificException: connectHelper:1735: Camera "0" disabled by policy (code 6)
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2387)
        at android.os.Parcel.createException(Parcel.java:2357)
        at android.os.Parcel.readException(Parcel.java:2340)
        at android.os.Parcel.readException(Parcel.java:2282)
        at android.hardware.ICameraService$Stub$Proxy.connectDevice(ICameraService.java:634)
        at android.hardware.camera2.CameraManager.openCameraDeviceUserAsync(CameraManager.java:511)
        ... 13 more

I know for sure that you can open the camera while phone is locked because I have APK that does this. And no need to use admin.

I have tried starting the foreground service because it is more priority, and I thought it helps, but no. And if phone unlocked -- works fine. Tested on Pixel 5 and HUAWEI p40 pro.

HELP ME PLEASE!


Solution

  • So, as a Muhammad Babar suggested to me, I can lower the target API level, and it will help. But I just found the right solution to the problem, if you interested -- read this docs:

    https://developer.android.com/guide/components/foreground-services