Is there any way to find whether the Android Camera is in use in code?
问题:
回答1:
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
}
回答2:
I don't know why this question is being asked several times, once you start to your own activity or application camera itself will be released as activity running for camera will be go in pause state.
回答3:
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.");
}
};
回答4:
You can try this method.if it returns true then camera is still in use by some app.
public boolean isCameraUsebyApp() {
Camera camera = null;
try {
camera = Camera.open();
} catch (RuntimeException e) {
return true;
} finally {
if (camera != null) camera.release();
}
return false;
}
Then show toast to user to restart the device as the camera needs restart.