1
votes

I tried this code to turn on/off the flash light:

Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab

and it worked for me, I mean it turned on the flash but now I can't to turn off the flash after press off button.

This is my code:

public class Main extends Activity
{
private Camera camera;
private Button buttonOn;
private Button buttonOff; 

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    buttonOn = (Button) findViewById(R.id.buttonOn);
    buttonOff = (Button) findViewById(R.id.buttonOff);

    buttonOn.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            try {
                 turnOn();
            } catch (Exception ex) {

            }
        }
    });

    buttonOff.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            try {
                 turnOff();
            } catch (Exception ex) {

            }
        }
    }); 
}

@Override
public void onPause(){
    super.onPause();
    if( camera != null )
       {
          camera.release();
          camera = null;
       }
       super.onPause(); 
}

@Override
public void onResume(){
    super.onResume();               
} 

@Override
public void onStart(){
    super.onStart();
}

@Override
public void onStop(){
    super.onStop();
}

@Override
public void onDestroy(){
    super.onDestroy();
}

@Override
public void onRestart(){
    super.onRestart();
}   

private void turnOff()
{               
        camera.stopPreview();
        camera.release();

   }

 private void turnOn() 
   {
            camera = Camera.open();  
            camera.startPreview();
            camera.autoFocus(new AutoFocusCallback() {
                        public void onAutoFocus(boolean success, Camera camera) {
                        }
                    });

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

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);

   }
}

In this way the led turns on after I click on buttonOff first. If I click on buttonOn before click on buttonOff it doesn't work. Once I do this then the led is on all the time but I can't turn it off when I click on buttonOff.

If I put this part of code:

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);

on 'turnOff' function instead where it is now, then the led is on only around 5 seconds. I tried using thread.sleep between both parts: Flash mode on and Flash mode off as I read in the post but it didn't work either.

Any idea about this?

Thanks in advanced.

1
A catch must never be empty! Fix that and log errors, then probably that will guide you to the problem.ESL

1 Answers

1
votes

According to the link you provided you need to call Camera.release() in your button...