1

hi i want to implement torch application in android so here is my code

    setContentView(R.layout.main);
    camera = Camera.open();
    if(camera == null)
        Toast.makeText(getBaseContext(),"CAMERA FAILED", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(getBaseContext(),"camera opened",Toast.LENGTH_LONG).show();
}
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                if(camera!=null)
                {
                     Toast.makeText(getBaseContext(),"came to volume up",Toast.LENGTH_LONG).show();
                     Parameters params = camera.getParameters();
                     params.setFlashMode( Parameters.FLASH_MODE_ON );
                     camera.setParameters(params);

                }
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_UP) {
                Parameters params = camera.getParameters();
                params.setFlashMode( Parameters.FLASH_MODE_OFF );
                camera.setParameters(params);
                camera.release();
                camera = null;
                Toast.makeText(getBaseContext(),"came to volume down",Toast.LENGTH_LONG).show();
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }

}

why this code is not working???

Durga
  • 1,191
  • 1
  • 12
  • 18
  • Do you get any error messages? – Neil Knight Apr 19 '11 at 11:01
  • no i havent got any error messages...and it is going to events as well but the problem is i couldnt make flash on and off with my code – Durga Apr 19 '11 at 11:02
  • What exactly isn't working about it. Is it throwing an error? Is it getting into the Key Event listener? You've got a lot of real estate in there, it'd be helpful to isolate what really is going wrong. – Brian Apr 19 '11 at 11:02
  • eventhough i am pressing volumeup button the flash is not switching on...please tell where i am doing wrong – Durga Apr 20 '11 at 06:43
  • You will have to do something like this: http://stackoverflow.com/questions/8876843/led-flashlight-on-galaxy-nexus-controllable-by-what-api hope it works – Pedro Rainho Mar 10 '12 at 21:47
  • You will have to do something like this: http://stackoverflow.com/questions/8876843/led-flashlight-on-galaxy-nexus-controllable-by-what-api hope it works – Pedro Rainho Mar 10 '12 at 21:48

4 Answers4

1

Durga,

I believe you want FLASH_MODE_TORCH not FLASH_MODE_ON when you press the volume up key.

Perhaps the following permissions may be required: FLASHLIGHT and CAMERA

Will Tate
  • 33,439
  • 9
  • 77
  • 71
  • i have tried FLASH_MODE_TORCH also but it didnt worked.and showing an error when i am pressing volume up or volume down in the logcat....the error shown when i am pressing volume up is ERROR/QualcommCameraHardware(95): Unexpected Flash Mode on Hal : -1 ERROR/QualcommCameraHardware(95): Set zoom=1 mZoom=1 and the error when i am pressing volume down is ERROR/QualcommCameraHardware(95): Set zoom=1 mZoom=1 can you tell me why these errors are coming?? – Durga Apr 20 '11 at 09:09
  • added permissions you may require, other than that perhaps your hardware doesn't support it? what phone are you working with? – Will Tate Apr 20 '11 at 12:24
0

William Tate was correct saying that you need FLASH_MODE_TORCH along with the two permissions, but the camera hardware doesn't get touched until you start a video preview.

This code will turn on the torch (though you may want to do some error catching in your actual app):

Camera c = Camera.open();
Camera.Parameters p = c.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
c.setParameters(p);
c.startPreview();

And the properly formatted permissions will look like this in your android manifest:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
thepenguin77
  • 499
  • 1
  • 6
  • 11
0

For Samsung you're required to have a surface view for the camera preview inorder for the torch mode to work.

DID you get it to work yet?

KITT
  • 230
  • 2
  • 3
  • 15
  • Have a class that extends SurfaceView and implements SurfaceView.Callback. In teh constructor, add a callback to the holder and set its type to PUSH_BUFFERS. For this class, initialize your Camera object in the surfaceCreated method and set your camera parameters there. In the surfaceDestroy, release camera object. In addition, implement additional methods to determine if Samsung device have LED and device LED is lit. In your activity, declare your surface view object add it to your layout, then setContentView. – KITT Jun 15 '11 at 08:18
0

Have a class that extends SurfaceView and implements SurfaceView.Callback. In teh constructor, add a callback to the holder and set its type to PUSH_BUFFERS.

For this class, initialize your Camera object in the surfaceCreated method and set your camera parameters there.

In the surfaceDestroy, release camera object.

In addition, implement additional methods to determine if Samsung device have LED and device LED is lit.

In your activity, declare your surface view object add it to your layout, then setContentView.

KITT
  • 230
  • 2
  • 3
  • 15