[英]Android camera fails in IntentService on Lollipop
I'm trying to take a picture in the background from IntentService. 我正在尝试从IntentService在后台拍照。 On Android 4.4 all is ok, but on Android 5.1 I've got an error:
在Android 4.4上一切正常,但在Android 5.1上却出现错误:
03-18 14:35:54.497 7659-8956/xyz.bringoff.proximityphoto.app E/InvisibleCameraService﹕ Can't use a camera: Fail to connect to camera service
03-18 14:35:54.497 7659-8956 / xyz.bringoff.proximityphoto.app E / InvisibleCameraService:无法使用相机:无法连接到相机服务
My code looks like this: 我的代码如下所示:
private void handleActionShot() {
mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
releaseCamera();
mCamera = getCameraInstance();
}
public Camera getCameraInstance() {
Camera c = null;
int numCams = Camera.getNumberOfCameras();
if (numCams > 0) {
try {
c = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
} catch (RuntimeException e) {
Log.e(TAG, "Can't use a camera: " + e.getMessage());
releaseCamera();
return null;
}
}
if (c != null) {
c.setParameters(getProperParametersForCurrentDevice(c));
c.lock();
}
return c;
}
I did not find a documented difference between camera requests on this two android versions. 在这两个android版本上,我没有发现相机请求之间的差异。
I have found a problem. 我发现了一个问题。 Receiver caught intent with STATE_OFFHOOK twice and respectively started IntentService twice.
接收器两次用STATE_OFFHOOK捕获了意图,并分别启动了两次IntentService。 I get a camera instance on the first start, so the second time I caught
onHandleIntent()
camera was already locked. 我在第一次启动时得到了一个相机实例,因此第二次捕获
onHandleIntent()
相机已经被锁定。 So, I added a check in onHandleIntent()
method 所以,我在
onHandleIntent()
方法中添加了一个检查
if (ACTION_SHOT.equals(action) && !mAlreadyRunning) {
mAlreadyRunning = true;
handleActionShot();
}
and now it works. 现在可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.