How to turn on the Android Flashlight

12,545

Solution 1

I thought I would update this with some bullet prof code that works on almost all 4.0+ devices.

public void turnOn() {
    camera = Camera.open();
    try {
        Parameters parameters = camera.getParameters();
        parameters.setFlashMode(getFlashOnParameter());
        camera.setParameters(parameters);

        camera.setPreviewTexture(new SurfaceTexture(0));

        camera.startPreview();
        camera.autoFocus(this);
    } catch (Exception e) {
        // We are expecting this to happen on devices that don't support autofocus.
    }
}

private String getFlashOnParameter() {
    List<String> flashModes = camera.getParameters().getSupportedFlashModes();

    if (flashModes.contains(FLASH_MODE_TORCH)) {
        return FLASH_MODE_TORCH;
    } else if (flashModes.contains(FLASH_MODE_ON)) {
        return FLASH_MODE_ON;
    } else if (flashModes.contains(FLASH_MODE_AUTO)) {
        return FLASH_MODE_AUTO;
    }
    throw new RuntimeException();
}

The real key is setting that fake SurfaceTexture so that the preview will actually start. Turning it off is very easy as well

public void turnOff() {
        try {
            camera.stopPreview();
            camera.release();
            camera = null;
        } catch (Exception e) {
            // This will happen if the camera fails to turn on.
        }
}

Solution 2

Test this :

if(camera == null){

camera = Camera.open();
parameters = camera.getParameters();

List<String> flashModes = parameters.getSupportedFlashModes();

    if(flashModes != null && flashModes.contains(Parameters.FLASH_MODE_TORCH)){

        //appareil supportant le mode torch
        parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(parameters);
    } else if (flashModes != null && flashModes.contains(Parameters.FLASH_MODE_ON)){

        //spécial samsung
        parameters.setFlashMode(Parameters.FLASH_MODE_ON);
        camera.setParameters(parameters);
        camera.startPreview();
        camera.autoFocus(new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) { }
        });
    } else {
        parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
    }  

        parameters.setFlashMode( Parameters.FLASH_MODE_OFF );
        camera.setParameters(parameters);
        camera.release();
        camera = null;

    } catch (RuntimeException e) {}

}//if

Solution 3

It seems like the developer of the Tiny Flashlight + LED app on the Android Market figured out how to make the flashlight work on LG Revolution.

Maybe you can contact him and ask? You can also check the permissions he is using in his app to try to make your app work!

Good luck!

Share:
12,545
MinceMan
Author by

MinceMan

Updated on July 19, 2022

Comments

  • MinceMan
    MinceMan over 1 year

    Update

    Check out my answer

    Original

    I'm trying to turn on the camera flashlight on the LG Revolution within my program. I use the torch mode method which works on most phones but not on LG phone. Does anyone know how to get it to work on LG's or specifically the Revolution?

    Here's my manifest:

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

    Here's my current code:

    public Camera camera = Camera.open();
        public Camera.Parameters Flash = camera.getParameters();
    

    With my on create:

                Flash.setFlashMode("torch");
                Parameters p = camera.getParameters();
                camera.setParameters(Flash);
                camera.startPreview();
    

    I've seen people use an auto focus but i don't know if that would work.

  • JoelC
    JoelC almost 9 years
    Welcome to StackOverflow! Generally answers will be better received if they include a description of the solution, no matter how obvious the answer may seem.