code-scanner
code-scanner copied to clipboard
Fail to connect to camera service
Using Android SDK v.24
W/CameraBase: An error occurred while connecting to camera 0: Service not available W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView E/AndroidRuntime: FATAL EXCEPTION: cs-init Process: com.fuib.bridgest, PID: 19022 java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.
(Camera.java:556) at android.hardware.Camera.open(Camera.java:372) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java:764) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java:741)
Code:
public class LoginActivity extends AppCompatActivity {
private CodeScanner mCodeScanner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
CodeScannerView scannerView = findViewById(R.id.scanner_view);
mCodeScanner = new CodeScanner(this, scannerView);
mCodeScanner.setCamera(CodeScanner.CAMERA_BACK);
mCodeScanner.setDecodeCallback(new DecodeCallback() {
@Override
public void onDecoded(@NonNull final Result result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), result.getText(), Toast.LENGTH_SHORT).show();
}
});
}
});
scannerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCodeScanner.startPreview();
}
});
}
@Override
protected void onResume() {
super.onResume();
mCodeScanner.startPreview();
}
@Override
protected void onPause() {
mCodeScanner.releaseResources();
super.onPause();
}
}
You should request camera permission on API >= 23 https://github.com/yuriy-budiyev/lib-demo-app/blob/master/app/src/main/java/com/budiyev/android/libdemoapp/codescanner/CodeScannerActivity.java
@yuriy-budiyev
Facing the same issue on some devices.
Android manifest has permission for Camera and there is run-time permission modal implemented in the Activity as well. Library works flawlessly in devices API >23.
Still found the same error.
Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service at android.hardware.Camera.(Camera.java:653) at android.hardware.Camera.open(Camera.java:494) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.initialize(CodeScanner.java) at com.budiyev.android.codescanner.CodeScanner$InitializationThread.run(CodeScanner.java)
Device info:
Device Brand: OPPO Model: X9009 Orientation: Portrait RAM free: 1.73 GB Disk free: 29.26 GB Operating System Version: 5.1 Orientation: Portrait Rooted: No
Have you added camera permission to manifest?
@yuriy-budiyev yes. Permission is added in manifest as well as runtime permission is also asked. Therefor Library is working in majority of devices in the developed App. I have found some devices mentioned earlier which don't support this library (Original Comment). I would recommend implementation of Camera2 API compatibility. My project is set to minSdkVersion = 19. Although CameraX is introduced in Android Jetpack, it requires minSdkVersion = 23, which will not benefit to this library.
I will update to Camera2 API when devices lower than 21 SDK become irrelevant
It is showing the issues with the camera permission
facing this issue. is there any update?
below Is the stack trace
Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.
Check camera permissions and update to 2.1.2 for better error handling.
E/AndroidRuntime: FATAL EXCEPTION: cs-init
Process: com.lightlogistics.vaast, PID: 5369
java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.
i have asked permission and it works but some times app crashes i am using 'com.budiyev.android:code-scanner:2.1.0' version
ask for runtime camera permission and you are good to go.