Search code examples
androidandroid-cameraandroid-camera2

Camera2 cameraManager.openCamera exception from time to time on some devices


I have an app which records videos, it has about 80K current installations (more than 100K downloads), Crashlytics statistics seems to be good - Crash-free users 99.66% and rating on Google Play is also OK - 4.5

But still for some of my users can happen the next errors (it may happened for such user once or a couple of times by now, so not often, but still happens from time to time):

  • android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): connectHelper:1578: Camera "0" disabled by policy

  • android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): Camera service is currently unavailable

  • java.lang.IllegalArgumentException: supportsCameraApi:2096: Unknown camera ID 0

  • android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): validateClientPermissionsLocked:1066: Caller "com.MY_APP_PACKAGE" (PID 10237, UID 21433) cannot open camera "1" when sensor privacy is enabled

on opening camera using camera2 API:

...
    cameraManager.openCamera(cameraId, stateCallback, mainHandler)`
} catch (e: Exception) {
    e.printStackTrace()
    openReportErrorDialog(e) // so a user could report this issue
...

Reported devices:

  • Vivo 1906
  • Wheatek BV5500Plus
  • Samsung SM-N975F
  • Samsung SM-G988W
  • Samsung SM-A520F
  • Motorola REVVLRY
  • HUAWEI VOG-L09
  • HUAWEI STK-LX1
  • HUAWEI MRD-LX1
  • HUAWEI FIG-LX1
  • Coolpad CP3669AS
  • Infinix X655C

Android versions: from 8 to 11 (app min SDK is 6)

So basically it can work fine for a specific user for some time, no issues when opening camera, but from time to time this exception can occur for such user

I don't have any idea why it happens. I can't reproduce this issue on my own devices (two Samsung, one Lenovo, one Huawei and one Xiaomi devices), only users can report such issue from time to time...

The most nonsense exception here is Unknown camera ID 0, because before opening camera I get list of available cameras, so it's not hardcoded, it's not possible that such camera id doesn't exist, and a user said that before this error that camera was working ok

UPDATE

Found this

https://developer.android.com/guide/components/foreground-services#bg-access-restrictions

So it seems if a foreground service was started by system (on BOOT_COMPLETED broadcast, declared in manifest) and not by app (if a user launch app) then it can't access camera and microphone.

So basically after rebooting a device we can't automatically start camera anymore without user interaction.

Really bad for dashboard camera applications for car drivers...

They added ACCESS_BACKGROUND_LOCATION, but there is no ACCESS_BACKGROUND_CAMERA and ACCESS_BACKGROUND_RECORD_AUDIO...


Solution

  • android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): connectHelper:1578: Camera "0" disabled by policy
    

    That either means the device has an enterprise policy installed that disables cameras for example (employers that don't want employees photographing stuff at work), or your app is trying to open the camera in the background, on more recent Android releases.

    If it's the policy, there's nothing you can do besides to tell the user that there's a policy in place and to complain to their admins.

    android.hardware.camera2.CameraAccessException: CAMERA_DISCONNECTED (2): Camera service is currently unavailable
    
    java.lang.IllegalArgumentException: supportsCameraApi:2096: Unknown camera ID 0
    

    These usually mean something has crashed in the camera stack - until things restart (takes a few seconds usually), all cameras will be reported as unknown. Ideally that'll never happen, but bugs unfortunately exist even on the best devices. A retry a few seconds later will probably work, unless the camera hardware on the phone is failing in some way that leads to a persistent crash.

    android.hardware.camera2.CameraAccessException: CAMERA_DISABLED (1): validateClientPermissionsLocked:1066: Caller "com.MY_APP_PACKAGE" (PID 10237, UID 21433) cannot open camera "1" when sensor privacy is enabled
    

    This means the OEM has some kind of 'shut off all cameras' feature like airplane mode is for radios. Like the enterprise policy above, this is something out of your control - the user needs to re-enable camera access, so all you can do is put up a dialog saying "cameras are disabled, sorry".