How to check if camera is opened by any application

后端 未结 3 1416
情话喂你
情话喂你 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: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.

提交回复
热议问题