4

Trying to capture photo from front camera when the app is in background.

I have the below code working in Android 6 and below.

Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
    Camera.getCameraInfo(camIdx, cameraInfo);
    if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        myCamera = Camera.open(camIdx);
    }
}

This works in Android 7 and above only if the app is in foreground. If the app is in background it shows the below error:

W/CameraBase: An error occurred while connecting to camera 1: Status(-8): '6: validateClientPermissionsLocked:915: Caller "packagename" (PID 10152, UID 32606) cannot open camera "1" from background'

W/System.err: java.lang.RuntimeException: Fail to connect to camera service

W/System.err:     at android.hardware.Camera.<init>(Camera.java:546)

W/System.err:     at android.hardware.Camera.open(Camera.java:392)

W/System.err:     at com.tracking_smartphone.TakePictureActivity.takePictureNoPreview(TakePictureActivity.java:77)

W/System.err:     at com.tracking_smartphone.TakePictureActivity.onCreate(TakePictureActivity.java:46)

W/System.err:     at android.app.Activity.performCreate(Activity.java:7144)

W/System.err:     at android.app.Activity.performCreate(Activity.java:7135)

W/System.err:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)

W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)

W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)

W/System.err:     at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)

W/System.err:     at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)

W/System.err:     at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)

W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)

W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)

W/System.err:     at android.os.Looper.loop(Looper.java:193)

W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6680)

W/System.err:     at java.lang.reflect.Method.invoke(Native Method)

W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)

W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

The code is called in an Activity which is invoked from a JobService.

Matt Ke
  • 3,599
  • 12
  • 30
  • 49
Srihari Karanth
  • 2,067
  • 2
  • 24
  • 34

1 Answers1

2

The Android documentation says:

Foreground Services Requirement - When does your app interact with the camera? On Android 9 (API level 28) and later, apps running in the background cannot access the camera. Therefore, you should use the camera either when your app is in the foreground or as part of a foreground service.

So if you want to be able to interact with the camera in the background, you need to do so from a so-called "foreground service", which is basically a background service with a notification that tells the user when it's active with a persistent notification. See the foreground service documentation for examples.

krzys_h
  • 123
  • 8