Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Library Barcode Scanner ZXing giving black Screen

I am using the barcode scanner library of ZXing. It is working fine for all of my testing devices except a Nexus 5 running Android 6. Starting the activity it gives only a black screen and that's all. So I thought, ok, perhaps there are some compatibility problems and I downloaded the official barcode app published by ZXing. But it works fine on that Nexus 5.

So actually I am a bit irritated what I could have done wrong...

In the Android Monitor I get the message:

android an error occurred while connecting to camera 0

I have searched for that status message but haven't found anything useful for me.

Has anyone a suggestion how to solve that problem ? Or a tip how to recognize the reason for the issue?

like image 251
jublikon Avatar asked Nov 27 '22 05:11

jublikon


1 Answers

Android 6 doesn't accept permisions from Manifest file you should ask for permission on runtime. somthing like this :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
                        getActivity().checkSelfPermission(Manifest.permission.CAMERA)
                                != PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(new String[]{Manifest.permission.CAMERA},
                            PERMISSIONS_REQUEST_ACCESS_CAMERA);
                } else {
                    mScannerView.startCamera();
                }

and then override onRequestPermissionResult :

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == PERMISSIONS_REQUEST_ACCESS_CAMERA) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                mScannerView.startCamera();
            }
        }
    }

hope this helps :)

like image 154
SepJaPro2.4 Avatar answered Nov 28 '22 18:11

SepJaPro2.4