BlueJam
BlueJam

Reputation: 43

How to check if the flashlight is on?

Is there any way to check once you have tried to turn the flashlight on if it has worked or not?

I'm using:

p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();

To turn the camera on. This works with most phones, but not with all phones so I need to know if it's worked or not.

Edit: Just to be clear, I need to know if turning the flash on has actually worked or not. Sometimes you need to use FLASH_MODE_ON instead of FLASH_MODE_TRUE, and sometimes you need to use FLASH_MODE_ON even when

List<String> flashModes = p.getSupportedFlashModes();
flashModes.contains(Parameters.FLASH_MODE_TORCH)

returns true.

Thanks for any help.

Upvotes: 2

Views: 2973

Answers (2)

Khaled AbuShqear
Khaled AbuShqear

Reputation: 1397

Works for me:

camera.getParameters().getFlashMode().equals(Parameters.FLASH_MODE_TORCH)

Returns true flashlight is on

Note: Camera class has been deprecated in API level 21. so this solution won't work for recent versions

Upvotes: 0

Niko
Niko

Reputation: 8163

First you need to check if the device supports flashlight:

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

Then you can safely assume that it will be turned on when you start preview.

Upvotes: 1

Related Questions