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

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.


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.


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.