Android: how to check for flash on the device?

How can I check the flash light on the device? Also want to know how can I turn on / off the flash? I put the code but didn’t work right now? I'm looking for it
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 tell me what should I use?
Thanks.

+4
source share
4 answers

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 details.

+20
source
 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; } 
+4
source

First you get the supported flash modes:

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

And then you check to see if this array contains the correct constants, such as: "auto", "on", "off".

Additional information: http://developer.android.com/reference/android/hardware/Camera.Parameters.html#FLASH_MODE_AUTO

+3
source

This may help to turn on / off the flash of the device completely. It gives me satisfaction, I hope you will be useful too.

Turn on the 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 the manifest file

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

See HERE for more details .

0
source

Source: https://habr.com/ru/post/1338679/


All Articles