我正在创建一个 LED 小部件。我可以打开灯,但是当我去关闭它时,我收到一个错误:无法访问相机。我不知道为什么..
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);
}
}
}
}
所以它会打开,但是当我尝试关闭它时会出错。