Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 849
Yogesh D Avatar asked Mar 26 '14 05:03

Yogesh D


3 Answers

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.

like image 133
Alex Cohn Avatar answered Nov 14 '22 10:11

Alex Cohn


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.

like image 9
Yogesh D Avatar answered Nov 14 '22 10:11

Yogesh D


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.

like image 2
Tash Pemhiwa Avatar answered Nov 14 '22 11:11

Tash Pemhiwa