Before calling HUAWEI Scan Kit, you must assign required permissions in the manifest file. Android provides two scanning permissions: CAMERA (camera permission) and READ_EXTERNAL_STORAGE (file reading permission).
- <!--Camera permission-->
- <uses-permission android:name="android.permission.CAMERA" />
- <!--File reading permission-->
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- <!--Used feature-->
- <uses-feature android:name="android.hardware.camera" />
- <uses-feature android:name="android.hardware.camera.autofocus" />
After specifying permissions in the manifest file, you will need to dynamically request the permissions in the code.
- // CAMERA_REQ_CODE is user-defined and is used to receive the permission verification result.
- ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, CAMERA_REQ_CODE);
You check whether the corresponding permission is enabled and determine whether to continue scanning the barcode.
- // Use the onRequestPermissionsResult function to receive the permission verification result.
- @Override
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
- // Check whether requestCode is set to the value of CAMERA_REQ_CODE during permission application, and then check whether the permission is enabled.
- if (requestCode == CAMERA_REQ_CODE && grantResults.length == 2 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- // Call the barcode scanning API to build the scanning capability.
- ...
- }
- }