Question

Is there any way to check is(/are) device camera(s) enabled ? If yes, what should I do ?

Était-ce utile?

La solution

Yes, there is that possibile, but it requires Device Admin Permissions . If you have that implemented, use that code :

DevicePolicyManager mDPM =
    (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
if(mDPM.getCameraDisabled(adminComponentName))
{
//do something if camera is disabled
}

Autres conseils

If by enabled, you mean open or currently in use, then yes, there is a way.

Camera.open() will give you an Exception if Camera is in use.

So you can make use of this to check if the camera is enabled, currently in use or even if there is actually a camera.

/** how to get the camera object savely */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // try to get camera
    }
    catch (Exception e){
        // Camera is not available (in use) or does not exist
    }
    return c; // null will be returned if unavailable, or non existent
}

If the camera is currently in use but you want to use it, simply call

Camera.release();

and then use it yourself.

You can use the package manager to check if the device has a camera (if that's what you mean by enabled):

http://developer.android.com/reference/android/content/pm/PackageManager.html#hasSystemFeature(java.lang.String)

PackageManager pm = context.getPackageManager();

if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
   // do your camera stuff
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top