hasSystemFeature(PackageManager.FEATURE_CAMERA) returns true for device with no camera

余生颓废 提交于 2019-12-05 12:32:36

问题


I have a application which uses camera functionality in it but part of its functionality can also run without camera feature. SO I have put this in my manifest.

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"  android:required="false"/> 

and in my code I check whether the device has camera or not using this

final boolean deviceHasCameraFlag = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);

Now I am testing my code on a tablet which runs Android 4.0(ICS) and has no camera. But still I get True value for the deviceHasCameraFlag. Is this weird or am I missing something.

I tried different things and even tried the same thing on Bluetooth feature as Tablet even doesn't have Bluetooth feature. It works fine for Bluetooth but gives me true for camera.


回答1:


Which device is it? The answer you get is a bug, and 4.0 is very old nowadays. Many tablets that still run this version were not crafted correctly, both hardware and software featuring multiple problems.

Regardless, you should always be prepared to handle failure on Camera.open() or Camera.open(0): for example, in some cases other software on your device will not release the camera gracefully.

So, in your case you have a false positive, you try to open the camera, it fails, and you continue as if there is no camera on the device, even if PackageManager thinks that PackageManager.FEATURE_CAMERA is availabe.




回答2:


Though I have accepted Alex's answer I still want to put this one collectively as what can be the best solution in such condition.

What I found was in case of some low standard android devices

pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)

returns true even if camera doesn't exist and that seems to be a device bug for me which in unchecked.

So whenever there is scenario that you need to check if camera exists for a device or not best practice is something that I am putting below (best practice as per my knowledge if there is something more interesting and best solution that this you are welcome to put it here on this post)

int numberOfCameras = Camera.getNumberOfCameras();
context = this;
PackageManager pm = context.getPackageManager();
final boolean deviceHasCameraFlag = pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);

if( !deviceHasCameraFlag || numberOfCameras==0 )
{
  Log.e(TAG, "Device has no camera" + numberOfCameras);
  Toast.makeText(getApplicationContext(), "Device has no camera",      Toast.LENGTH_SHORT).show();
  captureButton.setEnabled(false);
}
else
{
  Log.e(TAG, "Device has camera" + deviceHasCameraFlag + numberOfCameras);
}

In this I am checking both number of cameras as well as device has camera feature Boolean , so in any case it would not fail my condition.




回答3:


In my case I had this code:

public boolean hasCameraSupport() {
    boolean hasSupport = false;
    if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) { //<- this constant caused problems
        hasSupport = true;
    }
    return hasSupport;
}

and it kept returning false on a Genymotion device running Android 4.1.1 (API 16). Once I changed the constant PackageManager.FEATURE_CAMERA_ANY to PackageManager.FEATURE_CAMERA, my problems went away. I am guessing that not all devices/API levels support PackageManager.FEATURE_CAMERA_ANY.




回答4:


I got it you will try this one definitely it will work....

   import android.hardware.Camera;
   int numCameras = Camera.getNumberOfCameras();
    if (numCameras > 0) {
      System.out.println("camera");
    } else {
        System.out.println("No Camera");
    }



回答5:


Please try this code:

private boolean isDeviceSupportCamera() {
    if (getApplicationContext().getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

Still it does't work then please let me know



来源:https://stackoverflow.com/questions/22652189/hassystemfeaturepackagemanager-feature-camera-returns-true-for-device-with-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!