Reputation: 111
I want to create an application, and it needs to know when the camera is turned on, whether the camera application is open or if a third party app is using the camera. Is this possible?
Upvotes: 0
Views: 1218
Reputation: 729
Adding to Mây Và Bãos answer,
There are two ways of opening a camera - Camera.open() - gives the default camera - Camera.open(int id) - here you can give an ID (CameraInfo.CAMERA_FACING_BACK or CameraInfo.CAMERA_FACING_FRONT for most phones, i have not experimented with LGs 3d cameras :P)
If you get an exception or null camera object, that means - camera doesnt exist - camera is opened by some other application and it has not released.
In both cases you cannot do much other than to display some message in UI (like a toast)
If you get a valid camera , you can do operations on it , like startpreview , stop preview , take picture.
Almost all of the market apps that use camera release the camera when the activity the activity that uses camera goes to background. So that other apps can use it. That is the correct way as well
Hope this helps.
Regards, Shrish
Upvotes: 0
Reputation: 351
Try this code (from Android developer):
/** 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
}
Upvotes: 1