0

I am using camera to take pictures in the foreground service, which can be used long term like 5 to 10 hours. The problem is when another app starts using camera while the foreground service is running it stop taking pictures and user have to manually restart the service.

I am taking pictures in a loop ( I initializes the camera instance takes a picture then releases the camera and repeat). The solution I see here, is that at the start of each loop iteration I should add a check to see if the camera is currently in use.

How exactly can I check if the camera is in use by any other application or process on device? or is there a better approach to my problem?

Ahmad Malik
  • 117
  • 1
  • 1
  • 9
  • If can execute commands on your Android device via `adb`, you could try `adb shell dumpsys activity top | grep -e "camera.using.app"` to check if that camera using app is currently on top – nj2237 Mar 04 '22 at 08:28

2 Answers2

1

If you are using camera2 api, checkout CameraManager.AvailabilityCallback callback. it will inform you via callback when a cameraID is available/unavailable.

Wasim Ansari
  • 305
  • 1
  • 9
-2

This worked for me!!

if returns true, means camera is free

public boolean isCameraFree() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (RuntimeException e) {
            return false;
        } finally {
            if (camera != null) camera.release();
        }
        return true;
    }
Ahmad Malik
  • 117
  • 1
  • 1
  • 9
  • While this can be true, it is very expensive to open the camera for no other reason (takes a while, high power use for a few seconds). So I recommend looking at the AvailabilityCallback answer. – Eddy Talvala Mar 10 '22 at 01:22