문제

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.

도움이 되었습니까?

해결책

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.

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");
    }

For CameraX, if the FEATURE_CAMERA_ANY method is still returning true when there is no Camera on device, you can add the below method. So whether FEATURE_CAMERA_ANY returns true or false when CameraX is getting initialized, Below method will make sure to do what you want if a camera is actually not available on device.

private CameraSelector cameraSelector;
private ProcessCameraProvider cameraAvailableCheck;
private ListenableFuture<ProcessCameraProvider> cameraAvailableCheckFuture;

private void checkIfAnyCameraExist()
    {

        cameraAvailableCheckFuture = ProcessCameraProvider.getInstance(context);

        cameraAvailableCheckFuture.addListener(new Runnable() {
            @Override
            public void run() {
                try {
                    cameraAvailableCheck = cameraAvailableCheckFuture.get();

                    if ((cameraAvailableCheck.hasCamera(cameraSelector.DEFAULT_BACK_CAMERA) || cameraAvailableCheck.hasCamera(cameraSelector.DEFAULT_FRONT_CAMERA) ))
                    {
                        //Do what you want if at least back OR front camera exist

                    }

                    else
                    {
                        //Do what you want if any camera does not exist

                    }

                }

                catch (ExecutionException | InterruptedException | CameraInfoUnavailableException e)

                {
                    // No errors need to be handled for this Future.
                    // This should never be reached.
                }
            }

        }, ContextCompat.getMainExecutor(this));


    }

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top