Use camera flashlight in Android

Every device is a bit different. Samsung especially likes to make things complicated for app developers.

On the Galaxy Tab you should be good with:

Camera cam;
void ledon() {
    cam = Camera.open();     
    Parameters params = cam.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_ON);
    cam.setParameters(params);
    cam.startPreview();
    cam.autoFocus(new AutoFocusCallback() {
                public void onAutoFocus(boolean success, Camera camera) {
                }
            });
}

void ledoff() {
    cam.stopPreview();
    cam.release();
}

If that doesn't work then it might be a matter of setting FLASH_MODE_OFF initially and changing it after the startPreview.


You must not release the camera after setting the parameters. I found that flash is activated (in torch mode) after I have started the preview. ( Applies to motorola defy, 2.1 )

It is also a good idea to check supported flash modes, before trying to activate them.

Messing around with camera settings on android is darkest voodoo: Many devices behave differently and there seems to be no reliable way of targeting them all with one piece of code. Safest bet is to always set up your camera properly when your onResume() method is called. I would also consider doing the same in onConfigChange(), because at least Motorola screen locker can force your application into portrait mode, restarting it completely.

P.s. I suppose that when you close the camera, the native camera app is closed and then recreated in a fresh state.