erluxman
erluxman

Reputation: 19385

Android Marshmallow : java.lang.RuntimeException: Fail to connect to camera service

I am using https://github.com/dlazaro66/QRCodeReaderView (QR code scanner ) in my android application

My mainfest permission looks like this :

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

and in Gradle I have the following code :

 compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.gurkhatech.schoolmanagement"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

I have implemented the code in Java as :

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

But when i try to run that activity i get the following error log in the 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)

Upvotes: 1

Views: 4182

Answers (2)

kishor ramani
kishor ramani

Reputation: 78

Read this Steps from given link :

1: Request the permissions you need

2: Handle the permissions request response

click me

Upvotes: 5

J&#246;rn Buitink
J&#246;rn Buitink

Reputation: 2916

From Android 23 on, you need to request a permission every time your need it at runtime. So it is not enought to declare the permission in your manifest, you need to request it in code, too.

Please take a look at the google docs about this issue: http://developer.android.com/intl/es/training/permissions/requesting.html

Upvotes: 2

Related Questions