How to turn on the Android Flashlight
Asked Answered
A

5

10

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.

Aglitter answered 10/8, 2011 at 1:38 Comment(4)
So does no one know how to do this yet?Aglitter
I've tried every method I could find, so far no luck...Does LG publish documentation on their specific Android implementation?Satiated
Not that I've seen and they've never responded to the ticket I set up through their websiteAglitter
No I have not Pedro. And I don't think I can as I have no lg hardware. Hopefully someday someone will post it on this thread...Aglitter
A
9

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.
        }
}
Aglitter answered 2/8, 2014 at 17:54 Comment(7)
Would you please share the getFlashOnParameter() method as well?Sven
Oops on that! My bad, I'll get that up with 0-100 business days.Aglitter
Updated answer to contain this method.Aglitter
I expect a +1 Flyview!Aglitter
hehe I had already +1 :) Does the FLASH_MODE_AUTO ever work for any devices? I had built similar logic myself but only put in TORCH and ON.Sven
I think that it used to work for Samsung but I cannot remember.Aglitter
Works on my Umidigi A3 Pro, which required the line: camera.setPreviewTexture(new SurfaceTexture(0));Barytes
I
1

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!

Idem answered 16/8, 2011 at 3:57 Comment(1)
No response from Tiny Flasher :( Still don't know this one yet.Aglitter
R
1

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
Refute answered 22/12, 2011 at 2:12 Comment(4)
I'll test it out sometime this next week.Aglitter
This seemed to not work for me. I am adding the autofocus and I'll see if that works when I get a chance to us it on an LG phone. Though I had to do camera.autoFocus(new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { }Aglitter
So with the autoFocus the Spectrum™ by LG worked. I'm not sure it needed it though or if it's just a different phone, I'll find out and update.Aglitter
@ViToBrothers I second that :/ I have tried every possible variation on the Galaxy SII - no luck ... My code works on the Sony Xperia S though.Grievance
D
0

This worked well for LG Nexus:

camera = Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.setParameters(p);
camera.startPreview();
Disconsolate answered 6/1, 2015 at 6:23 Comment(0)
S
0
/*TESTED LG G4 */
public void flashOnOff(){
    List<String> flashModes = parameter001.getSupportedFlashModes();

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

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

        //spécial samsung
        parameter001.setFlashMode(Parameters.FLASH_MODE_ON);
        mCamera.setParameters(parameter001);

        mCamera.autoFocus(new AutoFocusCallback() {
            public void onAutoFocus(boolean success, Camera camera) { }
        });
    } else {
        parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameter001);
    } 
    if (!isFlashOn) {
        if (mCamera == null || parameter001 == null) {
            return;
        }

        parameter001 = mCamera.getParameters();
        parameter001.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(parameter001);
        try {
            mCamera.setPreviewTexture(new SurfaceTexture(0));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mCamera.startPreview();
        isFlashOn = true;

        // changing button/switch image

    }else if (isFlashOn) {
        if (mCamera == null || parameter001 == null) {
            return;
        }

        parameter001 = mCamera.getParameters();
        parameter001.setFlashMode(Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(parameter001);
        mCamera.stopPreview();
        isFlashOn = false;


    }
}
Sokil answered 15/6, 2015 at 12:35 Comment(1)
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.Assay

© 2022 - 2024 — McMap. All rights reserved.