tziuka
tziuka

Reputation: 545

getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) returns false

Hi guys!
I created a flashlight app and I checked if flash exists like this:

hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

    if (!hasFlash) {
        AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
        alert.setTitle("Error");
        alert.setMessage("Sorry, your device doesn't support flashlight!");
        alert.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                finish();
            }
        });
        alert.show();
        return;
    }

The problem is on some old devices which has the flash sensor but this method of checking returns false. (for example when I run it on Vertis 3510 You or Serioux RM X401 this checking method returns false and when I comment those lines it just works fine).
Is there another way of checking if device supports flash?
Thank you!

Upvotes: 0

Views: 1718

Answers (1)

Fahim
Fahim

Reputation: 12378

Use this code

public boolean hasFlash() {
        if (camera == null) {
            return false;
        }

        Camera.Parameters parameters = camera.getParameters();

        if (parameters.getFlashMode() == null) {
            return false;
        }

        List<String> supportedFlashModes = parameters.getSupportedFlashModes();
        if (supportedFlashModes == null || supportedFlashModes.isEmpty() || supportedFlashModes.size() == 1 && supportedFlashModes.get(0).equals(Camera.Parameters.FLASH_MODE_OFF)) {
            return false;
        }

        return true;
    }

Upvotes: 2

Related Questions