java.lang.RuntimeException:无法连接到摄像头服务(特定于设备)

时间:2016-05-28 04:00:42

标签: android android-camera android-permissions

我的设备问题" karbon sparkle v(android one)版本6.0

当我尝试打开QR扫描仪时:

//getting the camera permission for API 21 and above dynamically
        try {
            ActivityCompat.requestPermissions(QRActivity.this,
                    new String[]{Manifest.permission.CAMERA}, PackageManager.PERMISSION_GRANTED);
        } catch (Exception ignored) {


        }
        mQrCodeReaderView = (QRCodeReaderView) findViewById(R.id.qrdecoderview);
        mQrCodeReaderView.setOnQRCodeReadListener(this);   

并且这是第一次只有当app要求Marshmallow Camera权限时才会崩溃,这是我的设备日志。但是,如果我授予许可,它不会再次崩溃,但每次我尝试访问qr扫描仪时它仍然会一直要求许可。

这是我在logcat中的错误日志,即使它在我的java文件中似乎没有问题,因为它没有从logcat提供我的任何java文件的链接。

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.gurkhatech.schoolmanagement, PID: 32576
                                                                                     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)

这是我的Mainfest.xml相机使用权限和功能声明:

<uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

所以

1)任何想法如何解决我的具体崩溃修复问题 设备吗

2)以及如何修复app每次都要求获得权限而不是一次又一次地工作而不需要在android中进行其余的运行?

2 个答案:

答案 0 :(得分:0)

  

1)任何想法如何解决我的特定设备崩溃问题?

从Android 6.0开始,您的应用必须在运行时请求Permission,您可能知道。将您的代码与建议here的内容相匹配。 这是CAMERA PERMISSION的方法:

<强>请求:

if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.CAMERA)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.CAMERA},
                MY_PERMISSIONS_REQUEST_CAMERA);

        // MY_PERMISSIONS_REQUEST_CAMERA is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

只需覆盖onRequestPermissionsResult方法即可处理接受:

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CAMERA: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                // Launch your intent/activity

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

如果您的应用需要多个Danger Permissions,您应该只在严格要求时显示吐司许可,而不是用级联权限转让给用户压倒。

最后用以下方法标记你的方法:

@TargetApi(Build.VERSION_CODES.M)

注释,使您的代码无缝地在SDK上运行&lt; 23.0

  

2)以及如何修复应用程序每次请求权限而不是询问一次并且工作而不要求在android中运行其余的   吗

这对您的用户来说是最烦人的事情,您为什么要考虑这样做?如果用户想要撤消对您的应用的任何权限,他可以通过进入您的应用信息来执行此操作:

enter image description here

希望它有所帮助。

答案 1 :(得分:-1)

在调用startCamera()之前,您需要检查用户是否已授予访问摄像头的权限。例如,你可以开始做这样的事情:

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
        == PackageManager.PERMISSION_GRANTED) {
      initQRCodeReaderView();
    } else {
      requestCameraPermission();
    }

和那两种方法:

private void initQRCodeReaderView() {
    qrCodeReaderView = (QRCodeReaderView) content.findViewById(R.id.qrdecoderview);
    qrCodeReaderView.startCamera();
}

private void requestCameraPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
          ActivityCompat.requestPermissions(DecoderActivity.this,
              new String[] { Manifest.permission.CAMERA }, MY_PERMISSION_REQUEST_CAMERA);
    } 
  }

如果要查看此示例的工作示例,可以检查库示例中的示例项目。 https://github.com/dlazaro66/QRCodeReaderView/tree/master/samples