我花了几个小时来寻找解决这个问题的方法,但无济于事。
我建立了一个相机:
if(camera == null){
Log.d("CamTest", "Attempting to establish camera");
camera = Camera.open();
Log.d("CamTest", "Established Camera");
}else{
Log.d("CamTest","already had a camera? Releasing");
camera.release();
Log.d("CamTest","already had a camera? Released; Opening");
camera = Camera.open();
Log.d("CamTest","already had a camera? Opened");
onCreate函数内部。我还有一个拆卸功能,如下所示:
if (recording) {
recorder.stop();
recorder.reset();
recording = false;
Log.d("CamTest", "recording reset");
}
if(recorder!=null){
recorder.release();
recorder = null;
Log.d("CamTest", "Recorder Released");
}
if(camera!=null){
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
Log.d("CamTest", "Camera Released");
}
在onStop函数内部。
我第一次点击应用程序时一切正常(也就是说,导航到使用相机的片段,然后按“后退”按钮)。我得到了(预期)输出:
Attempting to establish camera
Established Camera
Recorder Released
Camera Released
但是,如果我立即重新输入与相机相关的片段,我会崩溃:
Attempting to establish camera
Fail to connect to camera service
Camera.open() returned: null (this is generated by a Log I put in)
我在AndroidManifest中的权限也很好。
我的理解是Camera.open()在以下情况之一时返回null:没有后置摄像头或后置摄像头当前正由另一个应用程序使用。好奇心是我确定正在调用camera.release(),如上面的日志所示。也许更奇怪的是:这个bug只发生在物理上的三星S3上(适用于模拟S3,以及其他物理测试设备)
我倾向于这是一个硬件问题,因为它似乎适用于其他设备,但我非常感谢获得意见。