26

How can I check if a device has a camera led (flashlight)? I am talking about devices with android OS?

I have seen solutions some solutions which talks about how to turn the led on and off but what will happen if the device doesn't even has a led.

for turning on the camera I am using camera.open()

vlio20
  • 8,955
  • 18
  • 95
  • 180

5 Answers5

48

The other answers

boolean hasFlash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

Does not work for the new 2013 Nexus 7. The following code will work:

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

        Camera.Parameters parameters;
        try {
            parameters = camera.getParameters();
        } catch (RuntimeException ignored)  {
            return false;
        }

        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;
    }
JohnnyLambada
  • 12,700
  • 11
  • 57
  • 61
Erik B
  • 2,810
  • 1
  • 34
  • 38
  • 2
    hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) does not work on Wildfire S. Your method works fine for me, tested on Nexus 4, Nexus 5, Xperia Z1, Note 3, Wildfire S and Galaxy S (i9000, no flash). – Toni Alvarez Feb 07 '14 at 01:46
  • 1
    First solution working on Samsung S5, the second not. – c0dehunter Oct 02 '17 at 17:44
  • @PrimožKralj Thanks for the info, which of the above checks failed on the S5? Do you have another solution that will work for both? – Erik B Oct 02 '17 at 21:15
  • 1
    I am not able to test on any other device right now, but will try in the following days and report back. What I did is I combined both methods in this answer: first checking with the first method and if it returns false (no flash), proceed with the second method. – c0dehunter Oct 03 '17 at 08:06
  • 1
    Both solution is not working in Lenovo tablet TB-X304L – JEGADEESAN S Oct 17 '19 at 19:06
6

getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) returns true if the device has flash. See this for more details

PC.
  • 6,870
  • 5
  • 36
  • 71
3

You should be able to check whether the flash is available by checking system features:

boolean hasFlash = this.getPackageManager()
                       .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

(provided you are in an Activity). If not, than use some sort of context in place of this.

P.S. Note that this information is quite easy to find if you actually try searching for it.

Aleks G
  • 56,435
  • 29
  • 168
  • 265
2
PackageManager pm = context.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Device has no camera!");
            return;
        }       
        camera = Camera.open();
        p = camera.getParameters();
        flashModes = p.getSupportedFlashModes();
if(flashModes==null){
                        Toast.makeText(getApplicationContext(), "LED Not Available",Toast.LENGTH_LONG).show();
                }else
                {
Toast.makeText(getApplicationContext(), "LED  Available",Toast.LENGTH_LONG).show();
}
appukrb
  • 1,507
  • 4
  • 24
  • 53
2

This is how I check if LED flash is available. Also you don't need to have the Camera permission to run this method.

private fun isLedFlashAvailable(context: Context): Boolean {
    // method 1
    if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
        return true
    }

    // method 2
    val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
    for (id in cameraManager.cameraIdList) {
        if (cameraManager.getCameraCharacteristics(id).get(CameraCharacteristics.FLASH_INFO_AVAILABLE) == true) {
            return true
        }
    }

    return false
}
Egis
  • 5,081
  • 5
  • 39
  • 61