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

有帮助吗?

解决方案

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
}

其他提示

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
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top