0

I am learning how to request permissions. now i want to use the Camera hardware, and i am trying to request this permission. I referred to the official website of Android developers, and i used the example posted on the website but i changed it a little as i need to request Android Camera hardware.

the manifest file does not contain any permissions and I expect when i run the below posted code, the "Toast" will show up as there is no permissions added in the manifest file. but what happened is, when i run the App it does not ask for adding any permission nor the "Toast" appears.

please explain to me why that is happeneing and how to set it right

code:

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

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
        Toast.makeText(this, "permission is not in the manifest file", Toast.LENGTH_SHORT).show();
    }  

} else {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
}
Amrmsmb
  • 1
  • 27
  • 104
  • 226

5 Answers5

0

You are trying to request the permissions in the else part... ie when the permission is already granted.

The outer else signifies that the permission is granted.... do the following instead

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

   if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
       Toast.makeText(this, "permission is not in the manifest file", Toast.LENGTH_SHORT).show();
   }  else{
           ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
   }

} else {
   Toast.makeText(this, "permission is granted already", Toast.LENGTH_SHORT).show();
}
Kushan
  • 5,855
  • 3
  • 31
  • 45
  • follow the answer by @Amit Desale for checking results when dealing with multiple permissions – Kushan Oct 06 '16 at 08:23
0
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
// here you need to request permission, or show explanation if needed
} else {
//we already have permission, no need to request
}

so the correct code would look like this:

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

    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
        Toast.makeText(this, "permission is not in the manifest file", Toast.LENGTH_SHORT).show();
    }  else {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMERA);
    }
}
Ankit
  • 6,554
  • 6
  • 49
  • 71
  • thanks ..but why if-statement of shouldShowRequestPermissionRationale is never executed??or in other words, when it will be executed – Amrmsmb Oct 06 '16 at 08:33
  • it will not execute if the permission is either denied and the user chooses don't show this again button or if someone has gone to the settings and removed the permission. Only executes if the permission is denied and either of the above isn't true – Kushan Oct 06 '16 at 13:37
0

Did you run the application in android m device ?

The permissions are asked only in android marshmallow devices for more info please refer here

For other android devices running in lower version other that marshmallow u need declare permissions in android manefest...

Please note that the permission dialog does not appears in android devices running lower version than android-m

Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38
0

private boolean checkWriteExternalPermission() {

String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
int res = getContext().checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);            

}

How permission can be checked at runtime without throwing SecurityException?

Community
  • 1
  • 1
0

You need to have those permissions to be listed in AndroidManifest.xml, even if you trying to implement Runtime permission model for Android M.

    if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)==PackageManager.PERMISSION_GRANTED){
        //do something
    }
    else
        requestPermissions(permissionsList,
                MY_PERMISSIONS_REQUEST_CODE);

Then after user action:

    @Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CODE:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                //do something
            } else{
                //do some other thing
            }
            break;

    }
}
Sanjeet
  • 2,385
  • 1
  • 13
  • 22