当我在这个特定的 Nexus 5 上打开相机时,我遇到了手机特定的问题。它是连接到相机时发生错误:0 --- 无法连接到相机服务
错误。在至少十几款其他手机上,一切都运行良好。在 Nexus 5 上使用相机的其他应用程序没有崩溃(表明并非所有应用程序都无法访问相机,只有我的应用程序无法访问相机)。
关于此还有很多其他问题,我已经尝试了所有这些问题。他们都谈论缺少权限,以及确保相机在使用后被销毁。
为了明确我的 list 请求并正确使用相机:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2" />
我在销毁时释放相机:
public void releaseCamera()
{
if (_camera != null)
{
_camera.stopPreview();
_camera.setPreviewCallback(null);
_camera.release();
_camera = null;
}
_surfaceHolder.removeCallback(this);
}
你能想到我得到这个的任何原因吗?我怀疑存在某种错误,因为我使用的是相机而不是相机2,但这是一个疯狂的猜测。 Reading the updates for API 6.0有一个关于相机的部分说:
In This release, the model for accessing shared resources in the camera service has been changed from the previous “first come, first serve” access model to an access model where high-priority processes are favored.
同样,如果不重新编写整个应用程序来使用camera2(不是一个选项),我无法确定发生了什么。
这是我打开相机的代码(以及除 Nexus 5 之外的所有其他手机上适用的代码)
private void setInitialCamera()
{
try
{
if (_isBackFacing == true)
{
_camera = Camera.open(0);
} else
{
_camera = Camera.open(1);
}
} catch (RuntimeException e)
{
Log.d("Runtime Exception","Error " + e);
e.printStackTrace();
} catch (Exception e)
{
Log.d("Camera Error: ", " Android is unable tell what the error was");
e.printStackTrace();
}
}
最佳答案
看来罪魁祸首与这部手机所经历的 6.0.1 更新有关。
虽然这种情况在其他手机上没有发生,但在失败的 Nexus 5 上却发生了。
6.0.1 更新允许用户为应用程序设置单独的权限。所以不知何故,相机的权限被关闭了。重新打开此功能解决了问题。
要到达那里,请转到设置 -> 应用程序 -> [应用程序名称] -> 权限
关于java - 连接相机时出错: 0 --- Fail to connect to camera service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35278742/