xRobot xRobot - 1 year ago 175
Android Question

How to grant permission for the camera?

I just need a way to get permission for using the camera of the smartphone.
I wrote this code, but the permission request dialog doesn't appear.

in the onCreate:


if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
} else {
new String[]{Manifest.permission.CAMERA}, 1);


public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

// my code after granted permission

} else {


Answer Source

requestPermissions() is not called because "ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)" is true

Step #1: Show some rationale (dialog, snackbar, something inline in your UI, whatever)

Step #2: When the user taps on something specific in that rationale UI (e.g., OK button), call requestPermissions()

Or, if you do not want to show rationale for why you are requesting the permission, do not call shouldShowRequestPermissionRationale().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download