Search code examples
androidandroid-camera

How to check, on Android, if device has flash light led


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()


Solution

  • 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;
        }