How to check if camera is opened by any application

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

提交回复
热议问题