3

I'm using Nexus S Android 2.3 stock version. Whenever I'm trying to set the flash to torch mode:

if (camera == null) {
    mCamera = camera = Camera.open();
}
final Parameters params = camera.getParameters();
params.setFlashMode(MODE_TORCH);
camera.setParameters(params);

nothing happens, the flash is not working at all. This piece of code works perfectly on nexus one.

I've found this - How to use camera flash/led as torch on a Samsung Galaxy Tab? and also this one - http://forum.xda-developers.com/showthread.php?t=923786 none of them seems to help.

Any idea what am I missing ?

Community
  • 1
  • 1
Efi MK
  • 1,022
  • 1
  • 11
  • 26
  • I can confirm that torch works in nexus S. My app posted here: https://market.android.com/details?id=droid.pr.coolflashlightfree is the prof of that. Also I have a nexus S and works. If you need help get in touch with me and I will sent you a working sample – Pedro Rainho Mar 01 '12 at 11:15
  • Pedro, that sounds great, can you post it here for others to see ? – Efi MK Mar 04 '12 at 21:54

1 Answers1

1
private Camera _camera;
protected static final String MODE_TORCH = Camera.Parameters.FLASH_MODE_TORCH;
protected static final String MODE_OFF = Camera.Parameters.FLASH_MODE_OFF;

private void initCamera(){
            if(this._camera == null){
            this._camera = Camera.open();
            this._camera.startPreview();
            }
    }

private void releaseCamera(){
        if(this._camera != null)
        {
            this._camera.stopPreview();
            this._camera.release();
        }

        this._camera = null;
    }

private void setCameraParameter(String value){
        if(this._camera != null){
            Camera.Parameters params = this._camera.getParameters();
            params.setFlashMode(value);
            this._camera.setParameters(params);
        }
    }

//To turn on just use:
private void turnOn(){
    initCamera();
    setCameraParameter(MODE_TORCH);
}

//to turn off just use:
private void turnOff(){
    setCameraParameter(MODE_OFF);
}

//To release resorces use:
private void releaseResources(){
   releaseCamera();
}

This will work on android 2.3. To work on android 2.3 and 4.0.3 you will have to play with surfaceView and surfaceHolder.

Pedro Rainho
  • 4,234
  • 1
  • 19
  • 21