6

How I check the flash light available on device?also want to know how can I On/Off the flash light? I have put the code but not working right now? I search out this
http://gitorious.org/rowboat/frameworks-base/commit/eb9cbb8fdddf4c887004b20b504083035d57a15f
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/com/android/server/LightsService.java#LightsService

Please can tell which I should use?
Thank You.

Sameer Z.
  • 3,265
  • 9
  • 48
  • 72
  • possible duplicate of [how to check if device has flash light led android](http://stackoverflow.com/questions/13413938/how-to-check-if-device-has-flash-light-led-android) – Mark J. Bobak Apr 09 '14 at 19:34

4 Answers4

22

You can use the following

context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

which will return true if a flash is available, false if not.

See http://developer.android.com/reference/android/content/pm/PackageManager.html for more information.

Al Sutton
  • 3,904
  • 1
  • 22
  • 17
  • 2
    I have my hands on cheap chinese android tablet, where this returns true, even though there is no flash support. I also found that getSupportedFlashModes() returns null in this case, so could probably be used to double check if flash is available. – SMGhost Jul 25 '13 at 07:46
  • 1
    2013 Nexus 7 also returns true. There is no flash. – Erik B Oct 25 '13 at 20:44
  • 2
    Nexus 7 returns true, and then getSupportedFlashModes() returns a list with one entry. A string that reads, "off". :-( – emrys57 Oct 29 '13 at 11:21
  • 1
    I mention how to fix for the Nexus 7 here: http://stackoverflow.com/questions/13413938/how-to-check-if-device-has-flash-light-led-android/19599365#19599365 – Erik B Apr 09 '14 at 19:26
  • Iiconbit nt-3702s also returning true. – AlexS Nov 20 '18 at 06:40
5
boolean hasFlash =this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

or

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;
    }
reza.cse08
  • 5,938
  • 48
  • 39
3

First you get supported flash modes:

camera = Camera.open(i);    // Introduced in API level 9
parameters = camera.getParameters();
String[] flashModes = parameters.getSupportedFlashModes();

And then you check if this array contains the correct constants like: "auto", "on", "off".

More info in: http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FLASH_MODE_AUTO

0

This can help full to turn on/off flash light of device. This is give me satisfaction, Hope be useful to you also.

Turn On camera Flash

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();

Turn Off camera Flash

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

Put this Permission in manifest file

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

For more detail you can go HERE.

Yog Guru
  • 2,074
  • 3
  • 28
  • 47
  • Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Taryn Dec 22 '12 at 11:29