7

My app requires to access CAMERA and WRITE_EXTERNAL_STORAGE permissions.

Once my app loads I want to ask user to allow both of these permissions one after another. I have this code:

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

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
    }


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

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

    }

Now once my app loads it asks for the first permission but never asks for the second until I reload the entire app again.

How do I ask both of these permissions from the user once app loads?

Levon
  • 1,681
  • 2
  • 18
  • 40
AlwaysConfused
  • 729
  • 2
  • 12
  • 37
  • check this link: http://inthecheesefactory.com/blog/things-you-need-to-know-about-android-m-permission-developer-edition/en – Matcoil Apr 18 '16 at 16:58
  • Possible duplicate of [How to check the multiple permission at single request in Android M?](https://stackoverflow.com/questions/34040355/how-to-check-the-multiple-permission-at-single-request-in-android-m) – Flimm Nov 02 '18 at 12:24

2 Answers2

5

You have to put all of the permissions into one String array:

new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA}

This way, users will be shown a dialog with all of the permissions and they can decide to deny or grant each permission individually. As soon as they are finished, your Activity will be called with onRequestPermissionsResult().

One of the parameters of this method is an array of type int named grantResults which you can evaluate to know which permissions were granted.

Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • @0X0nosugar: It works perfectly when I asks the permissions for first time. If I go to the settings and turn off one of the permissions(for example camera), then I open the app, it asks for both permissions instead of a single one. How to solve it? – Madhan Jan 30 '17 at 07:20
  • @Madhan - "it asks for both permissions" because you tell it to do so :) As soon as you notice that one permission is missing, you can ask for only this permission (if the user did not opt out of being asked...). I think it is a good idea anyway to only show a request for the newly missing permission. Else the user might decide to withdraw the other one for a change. So you need to determine what type of request to make. Look up PermissionChecker, it's quite useful. – Bö macht Blau Jan 31 '17 at 18:45
3

You can ask for more than one permission in requestPermissions(). It takes an array of permission names, not just a single permission.

Note, though, that whatever you include in that array will be asked for, even if you already have that permission.

So, you can do something like this:

  private static final String[] PERMS_TAKE_PICTURE={
    CAMERA,
    WRITE_EXTERNAL_STORAGE
  };

  private void gimmePermission() {
    if (!canTakePicture()) {
      ActivityCompat.requestPermissions(this,
        netPermissions(PERMS_TAKE_PICTURE), RESULT_PERMS_TAKE_PICTURE);
    }
  }

  private boolean hasPermission(String perm) {
    return(ContextCompat.checkSelfPermission(this, perm)==
      PackageManager.PERMISSION_GRANTED);
  }

  private boolean canTakePicture() {
    return(hasPermission(CAMERA) && hasPermission(WRITE_EXTERNAL_STORAGE));
  }

  private String[] netPermissions(String[] wanted) {
    ArrayList<String> result=new ArrayList<String>();

    for (String perm : wanted) {
      if (!hasPermission(perm)) {
        result.add(perm);
      }
    }

    return(result.toArray(new String[result.size()]));
  }

netPermissions() takes a String[] of the permissions you want and returns a String[] of the permissions that you do not yet hold, which you can then pass along to requestPermissions().

(code derived from this sample app)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491