0

I am using the camera flash light in my application, I was done coding for that, it's working on/off the light. but after 2 seconds it goes to off. If I press the on button again it was giving force close. This is the code i am using for this, please help me.

I want this like if user presses the on button light On, upto user press Off button.

private void processOffClick()  {

    //togglebutton.setButtonDrawable(R.drawable.offbutton);
    System.out.println("in off state");
    if( cam != null ){
        cam.stopPreview();
        cam.release();
    }
}
private void processOnClick()  {

    //togglebutton.setButtonDrawable(R.drawable.onbutton);
    System.out.println("in on state");       
    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) {
        }
    });      
}
Stephen
  • 1,737
  • 2
  • 26
  • 37
NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98
  • 3
    In which device you tested the code ?.`if i press again on button it was giving force close` -- post the logat. – Kartik Domadiya Nov 14 '11 at 07:18
  • 1
    Well I also face the same issue. You can refer my question : http://stackoverflow.com/questions/6939816/turn-on-off-camera-led-flash-light-in-samsung-galaxy-ace-2-2-1-galaxy-tab.. It has got more than 3.5k views but still no answer. – Kartik Domadiya Nov 14 '11 at 08:06
  • I'm not sure about that but you should try it, When you are telling the device to turn on the flash it turns it for a certain amount of time that I'm not sure if you can set it or not. Anyways, try to put the lines Parameters params = cam.getParameters(); params.setFlashMode(Parameters.FLASH_MODE_ON); cam.setParameters(params); in processOfClick and check it out, it might cancel the error – Baruch Nov 14 '11 at 08:07
  • then what is the solution for this.. – NagarjunaReddy Nov 14 '11 at 08:23
  • @Baruch u want place code in processOnClick or processOffClick, please correct u r comment. – NagarjunaReddy Nov 14 '11 at 08:34
  • @NagarjunaReddy : Some of the devices does not support `FLASH_MODE_TORCH` because that mode is not available in that device. Your phone Samsung Galaxy Ace doesnot have `FLASH_MODE_TORCH` – Kartik Domadiya Nov 15 '11 at 10:36

3 Answers3

1

put the lines:

Parameters params = cam.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_ON);
cam.setParameters(params);

in processOffClick instead of putting it in processOnClick like that:

boolean clicked = false;
private void processOffClick()  {

  //togglebutton.setButtonDrawable(R.drawable.offbutton);
  clicked = false;
  System.out.println("in off state");
  if( cam != null ){
      cam.stopPreview();
      cam.release();
  }
}
private void processOnClick()  {
  clicked = true;
  //togglebutton.setButtonDrawable(R.drawable.onbutton);
  System.out.println("in on state");       
  cam = Camera.open();     
  Parameters params = cam.getParameters();
  params.setFlashMode(Parameters.FLASH_MODE_ON);

  while(clicked)  { 
     cam.setParameters(params);

     cam.startPreview();
        cam.autoFocus(new AutoFocusCallback() {
           public void onAutoFocus(boolean success, Camera camera) {
        }
     });
  } 
}

It might work, i didn't check the code

I added a while loop so it would hold the flash and the focus until its unclicked.

RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
Baruch
  • 1,618
  • 5
  • 23
  • 42
  • light working ,but here the screen was not able to display (buttons are not displaying ).i think it goes to camera preview, i want only light. – NagarjunaReddy Nov 15 '11 at 04:34
  • You#re starting the preview of the camera over and over and over again oO ? This seems rather odd - without stopping it in between. My point being, that it doesn't seem very "healthy" to torture the camera like this :O ? – AgentKnopf Dec 19 '12 at 18:40
1

My experience says, that flash mode ought to be "TORCH" ( if supported ) and it is started only when you start preview. However, cameras behave very differently on different devices and not always as advertised in their capabilities descrioptors

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35
0

1.Turn on

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

2. Turn off

camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

And, put following permission on AndroidManifest.xml.

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

see this one http://www.mkyong.com/android/how-to-turn-onoff-camera-ledflashlight-in-android/

NagarjunaReddy
  • 8,621
  • 10
  • 63
  • 98