Question

I am creating a led widget. I can get the light to turn on, but when I go and turn it off I get an error: Can't access camera. I am not sure why..

public static void setLightMode(Context context)
{
    Log.i(TAG,"Opening Camera");
    //cam = Camera.open();
    Log.i(TAG,"in setLightMode");
    if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
    {
        Log.i(TAG,"CAMERA EXISTS");
        cam = Camera.open(); //cam is a global value set to null

        if(cam != null)
        {
            Log.i(TAG,"CAMERA NOT NULL");
            Parameters p = cam.getParameters();
            if(!LIGHT_STATE)
            {
                Log.i(TAG,"TURNING LED ON");
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cam.setParameters(p);
                cam.startPreview();
                setLightState(true);
            }
            else
            {
                Log.i(TAG,"TURNING LED OFF");
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cam.setParameters(p);
                cam.stopPreview();
                setLightState(false);
            }
        }
    }
}

So it will turn on, but it runs into an error when i try to turn it off.

Was it helpful?

Solution

As per item number 10 in the Camera documentation, you need to release() your Camera when you are done with it.

If you don't, then the phone will think that the Camera is still in use, and will not allow other processes (including your own) to open() the Camera again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top