Android Marshmallow:java.lang.RuntimeException:无法连接到相机服务

时间:2016-02-03 08:56:09

标签: android android-camera qr-code android-6.0-marshmallow

我在我的Android应用程序中使用https://github.com/dlazaro66/QRCodeReaderView(QR码扫描程序)

我的主要许可是这样的:

<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();
    }

但是当我尝试运行该活动时,我在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$3.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)

2 个答案:

答案 0 :(得分:5)

从给定链接阅读此步骤:

1:请求您需要的权限

2:处理权限请求响应

click me

答案 1 :(得分:2)

从Android 23开始,您需要在运行时每次需要时请求权限。 因此,不需要在清单中声明权限,您也需要在代码中声明它。

请查看有关此问题的Google文档: http://developer.android.com/intl/es/training/permissions/requesting.html