Is there any way to find whether the Android Camera is in use in code?
Is there any way to find whether the android camera is in use?
Yes, Camera.open()
will give you an Exception if Camera is in use.
From the docs,
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
I know this is a really old question, but I stumbled upon it with a google search wondering about the same thing. With the newer versions of android, you can register the CameraManager.AvailabilityCallback
to determine if the camera is in use or not. Here's some example code:
import android.hardware.camera2.CameraManager;
// within constructor
// Figure out if Camera is Available or Not
CameraManager cam_manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
cam_manager.registerAvailabilityCallback(camAvailCallback, mHandler);
CameraManager.AvailabilityCallback camAvailCallback = new CameraManager.AvailabilityCallback() {
public void onCameraAvailable(String cameraId) {
cameraInUse=false;
Log.d(TAG, "notified that camera is not in use.");
}
public void onCameraUnavailable(String cameraId) {
cameraInUse=true;
Log.d(TAG, "notified that camera is in use.");
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With