我正在使用Camera for Barcode scanner App和On Some devices(LG G Flex,Asus Nexus 7)获取:Android运行时例外 - 无法连接相机服务。 以下是来自menifest文件的片段:
`uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.CAMERA"
....`
我在暂停,停止和摧毁时释放相机。
/**
* Restarts the camera.
*/
@Override
protected void onResume() {
super.onResume();
try {
startCameraSource();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Stops the camera.
*/
@Override
protected void onPause() {
super.onPause();
if (mPreview != null) {
mPreview.stop();
}
}
/**
* Releases the resources associated with the camera source, the associated detectors, and the
* rest of the processing pipeline.
*/
@Override
protected void onDestroy() {
super.onDestroy();
if (mPreview != null) {
mPreview.release();
}
}
仍然超过运行时异常,我没有以上两个设备,所以我可以重现。 这个问题有没有解决方案?
答案 0 :(得分:0)
您好需要在清单文件中添加以下权限
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_REQUEST_CODE);
}
如果您使用的是Android M权限模型,则首先需要在运行时检查应用程序是否具有此权限,并且必须在运行时提示用户获取此权限。您在清单上定义的权限不会在安装时自动授予。
@Override
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Now user should be able to use camera
}
else {
// Your app will not have this permission. Turn off all functions
// that require this permission or it will force close like your
// original question
}
}
}
您需要回调对话框结果:
{{1}}