Android camera runtime permission error?

I'm trying to use the Camera2 API in my application, even if I use the following code to check the runtime camera permissions

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
                cameraManager.openCamera(cameraId, stateCallBack, null);

            } else {
                if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA))
                    Toast.makeText(getApplicationContext(), "PLease allow the app to use camera app", Toast.LENGTH_LONG).show();

            }
            ActivityCompat.requestPermissions(CaptureImageActivity.this,new String[]{"android.manifest.permissin.CAMERA"}, CAMERA_REQUEST_RESULT);

        } else {
            cameraManager.openCamera(cameraId, stateCallBack, null);
        }

@Override
public void onRequestPermissionsResult(int requestCode, String[] permission, int[] grantResult) {

    switch (requestCode) {

        case CAMERA_REQUEST_RESULT:
            if (grantResult[0] == PackageManager.PERMISSION_GRANTED) {

                try {
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                      //this method is created because of openCamera method below i don't understand why this method is created
                        return;
                    }
                    cameraManager.openCamera(cameraId, stateCallBack, null);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
            if (grantResult[0] != PackageManager.PERMISSION_GRANTED)
                Toast.makeText(getApplicationContext(), "camera is not granted", Toast.LENGTH_LONG).show();


            break;
        default:
            super.onRequestPermissionsResult(requestCode, permission, grantResult);
            break;
    }
}

I also have permissions contained in the androidmanifest. XML file

 <uses-permission android:name="android.permission.CAMERA" />

However, when I run my application, the permission dialog box is not displayed, but the camera is not granted toast display

1) Why is the permission dialog box not displayed?

2) Even if there is no dialog to show how the camera is not granted toast? I searched a lot, but it didn't help!

resolvent:

This is the working runtime permission for the camera API

 private static final int PERMISSIONS_REQUEST_CAPTURE_IMAGE = 1;
 if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        // User may have declined earlier, ask Android if we should show him a reason

        if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
            // show an explanation to the user
            // Good practise: don't block thread after the user sees the explanation, try again to request the permission.
        } else {
            // request the permission.
            // CALLBACK_NUMBER is a integer constants
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAPTURE_IMAGE);
            // The callback method gets the result of the request.
        }
    } else {


    }
@Override
    public void onRequestPermissionsResult ( int requestCode, String[] permissions,
    int[] grantResults){
        switch (requestCode) {
            case PERMISSIONS_REQUEST_CAPTURE_IMAGE: {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted 

                    Log.d("", "permission granted success");


                } else {
                    // permission denied

                    Log.d("", "permission denied");
                }
                return;
            }
        }
    }

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>