0

I have a problem with a flashlight app Android 4.0.4 Nexus S i9020. I have tried dozens of suggestions posted here on stackoverflow but nothing worked for me. The app worked with Android version 2.3.6 but since 4.0.4 the torch has stopped working.

Here is my impl and the logcat output.

@Override
protected void onResume()
{
    super.onResume();
    _Camera = Camera.open();
}

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

//called within runnable and post to a handler
private void processOffClick()
{

    if (_Camera != null)
    {
        Parameters params = _Camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_OFF);
        _Camera.setParameters(params);
        _Camera.stopPreview();
    }
}

//called within runnable and post to a handler
private void processOnClick()
{
    if (_Camera != null)
    {
        Parameters params = _Camera.getParameters();
        params.setFlashMode(Parameters.FLASH_MODE_TORCH);
        _Camera.setParameters(params);
        _Camera.startPreview();
    }
}

I have also tried to execute the onclick offclick methods without runnables.

In Logcat the folling error occurs after onclick is performed.

 04-07 14:10:02.719: E/CameraHardwareSec(82): preview window is NULL!
 04-07 14:10:02.719: I/CameraHardwareSec(82): virtual android::status_t android::CameraHardwareSec::startPreview() : deferring

There are some camera apps in the market which work with my phone. So there must be some way to get the flashlight on.

I also tried to add an SurfaceView/Holder but it did not work. Maybe I did something wrong.

Cheers Karim

Karim E.
  • 1
  • 1

1 Answers1

0

you are doing everything ok, in version 4.0+ you need a surface view. Declare one in your layout.xml

then do something like this:

implement SurfaceHolder.Callback

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //init your layout
        this._surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
        this._surfaceHolder = this._surfaceView.getHolder();
        this._surfaceHolder.addCallback(this);
        this._surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override
protected void onResume()
{
    super.onResume();
    _Camera = Camera.open();
    this._camera.startPreview();
    this._camera.setPreviewDisplay(this._surfaceHolder);
}


@Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //this._surfaceHolder = holder;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        //this._surfaceHolder = null;
    }
Pedro Rainho
  • 4,234
  • 1
  • 19
  • 21