0

I have a simple application with a button to turn on/off camera flash:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button)findViewById(R.id.btnFlash);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(!isLight)
                {
                    switchON();
                }
                else
                {
                    switchOFF();
                }
            }
        });
    }

If the flash was off, it will turn on and else, turn off. Yes, it works well. The problem is: - Firstly, I pressed the button to turn on, after that, I rotate my device and then pressing again to turn off -> Application crash.

Fatal Exception: main - Runtime Exception: Fail to connect to camera service

These are 2 functions to turn on/off

public void switchON()
    {
        camera = Camera.open();
        Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        camera.startPreview();
        isLight = true;
    }

    public void switchOFF()
    {
        Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
        camera.release();
        isLight = false;
    }
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Dark Light
  • 933
  • 1
  • 10
  • 20

2 Answers2

1

Try This (I re-wrote the code):

public class YourClass extends Activity {

    private Button button;
    private Camera camera;
    private boolean isLight=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button)findViewById(R.id.btnFlash);
        button.setOnClickListener(new OnClickListener() {

         @Override
            public void onClick(View v) {
                if(!isLight)
                {
                    switchON();
                }
                else
                {
                    switchOFF();
                }
            }
        });


   }

    @Override
    protected void onResume() {
        super.onResume();
        try{
            camera = Camera.open();
        } catch( Exception e ){
            e.printStackTrace();
        }
    }

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

    private void switchOFF(){
        if( mCamera != null ){
            Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        isLight = false;
        }
    }

    private void switchON(){
        if( mCamera != null ){
            Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_TORCH);
        camera.setParameters(p);
        isLight=true;
        }
    }
}
gilonm
  • 1,829
  • 1
  • 12
  • 22
  • but add your code into my app, but when I rotate my device, flash was off even I haven't pressed the button yet??? – Dark Light Feb 26 '14 at 09:20
  • Please explain better what you mean - in what state was your flash before and after device rotation.. thanks – gilonm Feb 26 '14 at 09:24
  • I want when I press button, if flash was off, it will be on, and else, on to off. But when I add your code into my application: Firstly, when I press button, it's ON -> OK. And then, I rotate my device, the flash turn OFF??? (I didn't press button anymore) – Dark Light Feb 26 '14 at 09:29
  • Remove `onPause()` override too. – gilonm Feb 26 '14 at 09:46
  • when remove onPause(), application still Crash :( – Dark Light Feb 26 '14 at 09:53
  • Fatal Exeption: main - Runtime Exception: Fail to connect to camera service – Dark Light Feb 26 '14 at 10:01
  • I tried, but if you use camera.release() in onPause, the flash will turn off automatically when roration. – Dark Light Feb 26 '14 at 10:34
0

Try:

 public void switchOFF()
    {

    if(camera == null)
    {
        camera = Camera.open();
    }
        Parameters p = camera.getParameters();
        p.setFlashMode(Parameters.FLASH_MODE_OFF);
        camera.setParameters(p);
        camera.stopPreview();
        camera.release();
        isLight = false;
    }
Dan Cuc
  • 76
  • 4