0

What I would do is simply turn on flash led of my phone by press a button. As I could read, it appear too much simple, but code I've found doesn't work!

This is how I turn on led at click on button: +

private void cameraOn() {
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();

    torch_button.setText("Switch off");

    isTorchOn = true;
}

Params and camera object was initialized inside onCreate method. No error is thrown, but light doesn't switch on. what's wrong?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
giozh
  • 9,868
  • 30
  • 102
  • 183
  • possible duplicate of [How turn on camera flash light programmatically in Android?](http://stackoverflow.com/questions/6068803/how-turn-on-camera-flash-light-programmatically-in-android) – Russ Wheeler Aug 16 '14 at 17:34

2 Answers2

1

Looks like this one has possibly already been answered

How to turn on camera flash light programmatically in Android?

But basically you need to have the correct permissions.

<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
         android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
         android:protectionLevel="normal"
         android:label="@string/permlab_flashlight"
         android:description="@string/permdesc_flashlight" />
Community
  • 1
  • 1
Russ Wheeler
  • 2,590
  • 5
  • 30
  • 57
0

The problem could be another application holding the camera. Your code seems to be right.

I had done it successfully with this code :

Parameters p = null;
    try {
            p = camera.getParameters();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (isLightOn) {
            Log.e("info", "turning off!");

            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(p);
            camera.stopPreview();
            isLightOn = false;

        } else {

            Log.e("info", "turning on!");

            p.setFlashMode(Parameters.FLASH_MODE_TORCH);

            camera.setParameters(p);
            camera.startPreview();
            isLightOn = true;

        }
Nadeem Iqbal
  • 2,357
  • 1
  • 28
  • 43