简体   繁体   中英

Application wont ask for camera permission

I am developing an Application for Android devices and I wanted to scan QR codes, but I have one problem, it just wont ask for the Camera permission, therefore the surfaceview just stays black.

My Code:

public class ScanQR extends AppCompatActivity {

private SurfaceView surfaceView;
private CameraSource cameraSource;
private TextView textView;
private BarcodeDetector barcodeDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scan_qr);
    surfaceView = (SurfaceView) findViewById(R.id.scanQR);
    textView = (TextView) findViewById(R.id.tvScanQr);

    barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();

    cameraSource = new CameraSource.Builder(this, barcodeDetector).setRequestedPreviewSize(640, 480).build();

    surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                return;
            } try {
                cameraSource.start(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            cameraSource.stop();
        }
    });

    barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
        @Override
        public void release() {

        }

        @Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            SparseArray<Barcode> qrCodes = detections.getDetectedItems();

            if(qrCodes.size() > 0){
                textView.post(new Runnable() {
                    @Override
                    public void run() {
                        Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(1000);
                        textView.setText(qrCodes.valueAt(0).displayValue);
                    }
                });
            }
        }
    });


}

Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.estcers.partygooo">

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity" />
    <activity android:name=".Login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".CreateParty" />
    <activity android:name=".SearchUser" />
    <activity android:name=".UserInfo" />
    <activity android:name=".Friends" />
    <activity android:name=".PendingFriends" />
    <activity android:name=".Parties" />
    <activity android:name=".Invitations" />
    <activity android:name=".Entries" />
    <activity android:name=".PendingAssistances" />
    <activity android:name=".PartyInfo" />
    <activity android:name=".GetQR" />

    <meta-data
        android:name="com.google.android.gms.vision.DEPENDENCIES"
        android:value="barcode" />

    <activity android:name=".ScanQR"></activity>
</application>

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScanQR"
android:orientation="vertical">

<SurfaceView
    android:layout_width="match_parent"
    android:layout_height="300sp"
    android:id="@+id/scanQR"
    android:layout_gravity="center"
    android:layout_marginTop="100sp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tvScanQr"
    android:text="AAAAAAAAAAAAAAAA"
    android:textSize="20sp"
    android:layout_gravity="center"
    android:layout_marginTop="20sp"
    android:gravity="center"/>

If anyone could help me with this problem I would be really grateful, I just have no clue of what is going on.

I also have the implementation: "implementation 'com.google.android.gms:play-services-vision:15.0.2'" and google() .

Thanks for any help!

When you're about to open the camera, include the following check:

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(YourCameraActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CAMERA_PERMISSION);
                return;
            }

and handle the result with:

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == REQUEST_CAMERA_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
                // close the app
                Toast.makeText(CameraActivity.this, "The app requires that permission in order to work properly!", Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }

and in your manifest:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM