Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to connect to camera service #24

Closed
Kyson opened this issue Jul 6, 2015 · 8 comments
Closed

Failed to connect to camera service #24

Kyson opened this issue Jul 6, 2015 · 8 comments

Comments

@Kyson
Copy link

Kyson commented Jul 6, 2015

hi,
i write codes as following,same to your demo

  1. find QRCodeReaderView in xml
  2. add listener
  3. onresume startpreview and onpause stop it

i have two activity,both need to read qrcode,and when i jump first activity to another ,a problem occurs:Failed to connect to camera service.

please help.thanks a lot

@Philip-Nunoo
Copy link

@Kyson noticed with android M you'd need to turn on camera permission with the device

@nielsswinkels
Copy link

I'm getting this sometimes as well, and also other apps that use the camera can't connect to it then. So far the only solution is rebooting the phone. I think we should do mydecoderview.getCameraManager().getCamera().release(); in onPause() like it says in the Android documentation: https://developer.android.com/reference/android/hardware/Camera.html

But I haven't figured out yet how to open the camera again in onResume().

@janskod
Copy link

janskod commented Mar 1, 2016

Got the same problem on Nexus 5 and S3 Mini. I also guess its because a release is not called in the pause func. In my scenario i solved it by always calling finish() in on pause, which will call the destroySurface method. Inside the destroySurface method the release is called. But this only works if you want the QR Activity always be closed if paused or so. A more clever solution would be nice.

@awadeyar
Copy link

awadeyar commented May 19, 2016

Hi,

In Android Marshmellow 6.x version the library gives the error and app crashes.

* FATAL EXCEPTION: main
Process: com.jnj.pave.stage, PID: 15782
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.(Camera.java:518)
at android.hardware.Camera.open(Camera.java:360)
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:583)
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:5466)
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)
*

Anybody has solution to this issue?

@milanPansuriya
Copy link

milanPansuriya commented Jun 14, 2016

Hey,
My App crashes in many Devices with the Error below:
java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.native_setup(Native Method) at android.hardware.Camera.<init>(Camera.java:407) at android.hardware.Camera.open(Camera.java:325) 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:78) at com.dlazaro66.qrcodereaderview.QRCodeReaderView.surfaceCreated(QRCodeReaderView.java:90) at android.view.SurfaceView.updateWindow(SurfaceView.java:572) at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:232) at android.view.View.dispatchWindowVisibilityChanged(View.java:8113) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewGroup.dispatchWindowVisibilityChanged(ViewGroup.java:1077) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1257) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1020) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5762) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:771) at android.view.Choreographer.doCallbacks(Choreographer.java:574) at android.view.Choreographer.doFrame(Choreographer.java:544) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:757) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:149) at android.app.ActivityThread.main(ActivityThread.java:5257) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609) at dalvik.system.NativeStart.main(Native Method)
Your help will be appreciable.

@awadeyar
Copy link

Hi milanPansuriya are you running the app on Android 6.XX?
If yes, it is because of the camera permission issue of Android 6.XX version.

You can use this code in your activity on create method.

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M){
if (ContextCompat.checkSelfPermission(PaveMainActivity.this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(PaveMainActivity.this,
                    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.

                ActivityCompat.requestPermissions(PaveMainActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        1);

            } else {

                // No explanation needed, we can request the permission.
                //Toast.makeText(getActivity(),"else",Toast.LENGTH_LONG).show();
                ActivityCompat.requestPermissions(PaveMainActivity.this,
                        new String[]{Manifest.permission.CAMERA},
                        1);

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

    } else{
        // do something for phones running an SDK before lollipop
    }

Use this code outside the on create method.

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

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

            } else {
                System.exit(0);
                finish();
                finishAffinity();
            }
            return;
        }

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

@dlazaro66
Copy link
Owner

If you're running Android 5.0 or 6.0, this is not a QRCodeReaderView issue since you need to check the Camera permission in runtime before using the view (please check: http://developer.android.com/intl/training/permissions/index.html)

@Moctar-Haiz
Copy link

I came up with the same problem and I'm sharing how I fixed it. It may help some people.

First, check your Android version. If it is running on Android 6.0 and higher (API level 23+), then you need to :

  1. Declare a permission in the app manifest. Make sure to insert the permission above the application tag.

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

<application ...>
    ...
</application>
  1. Then, request that the user approve each permission at runtime

    if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // here, Permission is not granted ActivityCompat.requestPermissions(this, new String[] android.Manifest.permission.CAMERA}, 50); }

For more information, have a look at the API documentation here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants