Android棉花糖:java.lang.RuntimeException:无法连接到相机服务
Android Marshmallow : java.lang.RuntimeException: Fail to connect to camera service
我在我的 android 应用程序中使用 https://github.com/dlazaro66/QRCodeReaderView(二维码扫描仪)
我的 mainfest 权限是这样的:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
在Gradle中我有以下代码:
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.gurkhatech.schoolmanagement"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
我已将 Java 中的代码实现为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
mQrCodeReaderView = (QRCodeReaderView)findViewById(R.id.qrdecoderview);
mQrCodeReaderView.setOnQRCodeReadListener(this);
}
@Override
public void onQRCodeRead(String text, PointF[] points) {
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
}
@Override
public void cameraNotFound() {
}
@Override
public void QRCodeNotFoundOnCamImage() {
}
@Override
protected void onResume() {
super.onResume();
mQrCodeReaderView.getCameraManager().startPreview();
}
@Override
protected void onPause() {
super.onPause();
mQrCodeReaderView.getCameraManager().stopPreview();
}
但是当我尝试 运行 activity 时,我在 Logcat
中收到以下错误日志
02-03 14:32:33.055 12673-12673/com.abc.def I/PlatformSupportManager: Using implementation class com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface of interface com.google.zxing.client.android.camera.open.OpenCameraInterface for SDK 9
02-03 14:32:33.057 12673-12673/com.abc.def I/GingerbreadOpenCamera: Opening camera #0
02-03 14:32:33.058 12673-12673/com.abc.def W/CameraBase: An error occurred while connecting to camera: 0
02-03 14:32:33.061 12673-12673/com.abc.def D/AndroidRuntime: Shutting down VM
02-03 14:32:33.063 12673-12673/com.abc.def E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abc.def, PID: 12673
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:495)
at android.hardware.Camera.open(Camera.java:341)
at com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.open(GingerbreadOpenCameraInterface.java:57)
at com.google.zxing.client.android.camera.open.CameraManager.openDriver(CameraManager.java:77)
at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:110)
at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
at android.view.SurfaceView.onPreDraw(SurfaceView.java:177)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
从 Android23 开始,您每次在运行时需要时都需要请求权限。
所以仅仅在清单中声明权限是不够的,您还需要在代码中请求它。
请查看有关此问题的 google 文档:
http://developer.android.com/intl/es/training/permissions/requesting.html
从给定的 link 阅读此步骤:
1:请求您需要的权限
2:处理权限请求响应
我在我的 android 应用程序中使用 https://github.com/dlazaro66/QRCodeReaderView(二维码扫描仪)
我的 mainfest 权限是这样的:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
在Gradle中我有以下代码:
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.gurkhatech.schoolmanagement"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
我已将 Java 中的代码实现为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr);
mQrCodeReaderView = (QRCodeReaderView)findViewById(R.id.qrdecoderview);
mQrCodeReaderView.setOnQRCodeReadListener(this);
}
@Override
public void onQRCodeRead(String text, PointF[] points) {
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
}
@Override
public void cameraNotFound() {
}
@Override
public void QRCodeNotFoundOnCamImage() {
}
@Override
protected void onResume() {
super.onResume();
mQrCodeReaderView.getCameraManager().startPreview();
}
@Override
protected void onPause() {
super.onPause();
mQrCodeReaderView.getCameraManager().stopPreview();
}
但是当我尝试 运行 activity 时,我在 Logcat
中收到以下错误日志02-03 14:32:33.055 12673-12673/com.abc.def I/PlatformSupportManager: Using implementation class com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface of interface com.google.zxing.client.android.camera.open.OpenCameraInterface for SDK 9
02-03 14:32:33.057 12673-12673/com.abc.def I/GingerbreadOpenCamera: Opening camera #0
02-03 14:32:33.058 12673-12673/com.abc.def W/CameraBase: An error occurred while connecting to camera: 0
02-03 14:32:33.061 12673-12673/com.abc.def D/AndroidRuntime: Shutting down VM
02-03 14:32:33.063 12673-12673/com.abc.def E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.abc.def, PID: 12673
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:495)
at android.hardware.Camera.open(Camera.java:341)
at com.google.zxing.client.android.camera.open.GingerbreadOpenCameraInterface.open(GingerbreadOpenCameraInterface.java:57)
at com.google.zxing.client.android.camera.open.CameraManager.openDriver(CameraManager.java:77)
at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:110)
at android.view.SurfaceView.updateWindow(SurfaceView.java:582)
at android.view.SurfaceView.onPreDraw(SurfaceView.java:177)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:944)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2055)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
从 Android23 开始,您每次在运行时需要时都需要请求权限。 所以仅仅在清单中声明权限是不够的,您还需要在代码中请求它。
请查看有关此问题的 google 文档: http://developer.android.com/intl/es/training/permissions/requesting.html
从给定的 link 阅读此步骤:
1:请求您需要的权限
2:处理权限请求响应