code-scanner icon indicating copy to clipboard operation
code-scanner copied to clipboard

Failed to connect to Camera Service

Open himgupta229 opened this issue 6 years ago • 11 comments

Hi, I am using library version 2.1.0 and on Xiaomi device(Os 5.1.1) I am always getting below exception:

Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera. + 533(Camera.java:533) at android.hardware.Camera.open + 365(Camera.java:365) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize + 835(CodeScanner.java:835) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run + 812(CodeScanner.java:812)

himgupta229 avatar Dec 06 '19 11:12 himgupta229

[edited] I restarted the device and the app now works as intended. I reproduced the crash with a fresh install on a phone that never had this app, so my app couldn't have been the one not releasing the camera resource. I suspect it's the MIUI camera app that "forgets" to release the camera sometimes, as I only get crashes on Xiaomi devices with MIUI (Android One devices are not affected, I tested it)

[Original comment] Hi, I am also using library version 2.1.0 and have the same crash with a Xiaomi Redmi K20 (Android 10):

Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.(Camera.java:637) at android.hardware.Camera.open(Camera.java:458) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java:835) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:812)

zzzzzzzzoli avatar Feb 25 '20 03:02 zzzzzzzzoli

Hi, I am also using library version 2.1.0 and the same crash with Xiaomi Android version 10

maxmudovmustafa avatar Apr 13 '20 11:04 maxmudovmustafa

Hi, I am using library version 2.1.0 and on Xiaomi device(Os 5.1.1) I am always getting below exception:

Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera. + 533(Camera.java:533) at android.hardware.Camera.open + 365(Camera.java:365) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize + 835(CodeScanner.java:835) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run + 812(CodeScanner.java:812)

Because Android 10 not supported.

maxmudovmustafa avatar Apr 21 '20 06:04 maxmudovmustafa

I have the same issue Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.<init>(Camera.java:667) at android.hardware.Camera.open(Camera.java:503) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java:835) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:812)

dondell avatar Dec 17 '20 03:12 dondell

Facing the same issue, the problem occurs when I try to initialize the camera the second time in a row,

W: An error occurred while connecting to camera: 0 W: java.lang.RuntimeException: Fail to connect to camera service W: at android.hardware.Camera.(Camera.java:550) W: at android.hardware.Camera.open(Camera.java:390) W: at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java:835) W: at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:812) D: Camera initialization error: Fail to connect to camera service

iamya5in avatar Dec 31 '20 05:12 iamya5in

I have the same issue Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.<init>(Camera.java:667) at android.hardware.Camera.open(Camera.java:503) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java:835) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:812)

Did you found any solution yet?

iamya5in avatar Dec 31 '20 05:12 iamya5in

I haven't found solution yet.

On Thu, Dec 31, 2020 at 1:28 PM YasinEnu [email protected] wrote:

I have the same issue Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.(Camera.java:667) at android.hardware.Camera.open(Camera.java:503) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java:835) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:812)

Did you found any solution yet?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/yuriy-budiyev/code-scanner/issues/80#issuecomment-752849450, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA73IDB35TMTLRI634CAD7LSXQDWZANCNFSM4JWUFUFA .

dondell avatar Jan 04 '21 06:01 dondell

same here

 java.lang.RuntimeException: Fail to connect to camera service
        at android.hardware.Camera.<init>(Camera.java:747)
        at android.hardware.Camera.open(Camera.java:549)
        at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java:835)
        at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:812)

shamshadpattani avatar Feb 11 '21 12:02 shamshadpattani

the problem is with the mobile no giving camera permission to your app. if you went to setting -> app -> {your app} -> permissions and enabled camera permission it works fine. so i guess you got to ask for permission of camera first.

helpful links:

  • https://developer.android.com/training/permissions/requesting
  • https://www.techotopia.com/index.php/Kotlin_-_Making_Runtime_Permission_Requests_in_Android

michaelbelete avatar Feb 18 '21 00:02 michaelbelete

The library provides a mechanism for intercepting camera initialization errors. To do this, when initializing the CodeScaner, you must set the ErrorCallback using setErrorCallback (final ErrorCallback errorCallback). If an error (any) occurs during initialization, the library checks for the presence of this callback and, if not, throws an error, and if there is, processes it.

try {
                initialize();
            } catch (final Exception e) {
                releaseResourcesInternal();
                final ErrorCallback errorCallback = mErrorCallback;
                if (errorCallback != null) {
                    errorCallback.onError(e);
                } else {
                    throw e;
                }
            }

PavelLepikhov avatar Feb 26 '21 08:02 PavelLepikhov

Check camera permissions and update to 2.1.2 for better error handling.

yuriy-budiyev avatar Mar 08 '22 11:03 yuriy-budiyev