How to check if camera is opened by any application

后端 未结 3 1398
情话喂你
情话喂你 2020-12-01 14:30

Is there any way to check if the camera is open or not? I don\'t want to open the camera, I just want to check its status.

相关标签:
3条回答
  • 2020-12-01 15:08

    Looking into the source code of Camera, its JNI counterpart, and finally the native code for connecting a camera with the service, it appears that the only way of determining if the camera is in use is directly through the result of Camera::connect(jint).

    The trouble is that this native code is only accessible through the JNI function android_hardware_Camera_native_setup(JNIEnv*, jobject, jobject, jint), which sets up the camera for use when creating the Camera instance from Java in new Camera(int).

    In short, it doesn't seem possible. You'll have to attempt to open the camera, and if it fails, assume it is in use by another applicaiton. E.g.:

    public boolean isCameraInUse() {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (RuntimeException e) {
            return true;
        } finally {
            if (c != null) c.release();
        }
        return false;
    }
    

    To better understand the underlying flow of camera's native code, see this thread.

    0 讨论(0)
  • 2020-12-01 15:13

    You can check it using method Camera.open(cameraId).

    Creates a new Camera object to access a particular hardware camera. If the same camera is opened by other applications, this will throw a RuntimeException.

    Throws RuntimeException
    If opening the camera fails (For Example, if the camera is in use by another process or device policy manager has disabled the camera).

    Update:

    Example:

    public boolean isCameraUsebyApp() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (RuntimeException e) {
            return true;
        } finally {
            if (camera != null) camera.release();
        }
        return false;
    }
    

    You can use this method to use as Paul suggested but keep this thing in mind that this method first acquire the camera.

    If its acquire successfully then its mean that no other application is using this camera and don't forgot to release it again otherwise you will not able to acquire it again.

    Its its throws RuntimeException it means that camera is in use by another process or device policy manager has disabled the camera.

    0 讨论(0)
  • 2020-12-01 15:17

    If your device API version is higher than 21, CameraManager.AvailabilityCallback might be a good choice.

    You need to first obtain the camera manager of the system with the following code:

    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    

    Then, you need to register the AvailabilityCallback:

    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        manager.registerAvailabilityCallback(new CameraManager.AvailabilityCallback() {
            @Override
            public void onCameraAvailable(String cameraId) {
                super.onCameraAvailable(cameraId);
                //Do your work
            }
    
            @Override
            public void onCameraUnavailable(String cameraId) {
                super.onCameraUnavailable(cameraId);
                //Do your work
            }
        }, yourHandler);
    }
    

    This works better if API version is higher than 21. You can refer to CameraManager, CameraManager.AvailabilityCallback, and the whole package

    Trying to open the camera to check if exception is thrown works good if API level is lower than 23. In API level 23, camera service is different than before, from the official docs:

    Access to camera subsystem resources, including opening and configuring a camera device, is awarded based on the “priority” of the client application process. Application processes with user-visible or foreground activities are generally given a higher-priority, making camera resource acquisition and use more dependable.

    Active camera clients for lower priority apps may be “evicted” when a higher priority application attempts to use the camera. In the deprecated Camera API, this results in onError() being called for the evicted client. In the Camera2 API, it results in onDisconnected() being called for the evicted client.

    We can see that in API 23 or higher, trying to open the camera used by other app/process will seize the camera from app/process which was using it, instead of getting RuntimeException.

    0 讨论(0)
提交回复
热议问题