Grant permission required for EXTERNAL_STORAGE in Android M?

30,875

Solution 1

I agree with Guillaume Perrot 's answer. I have met the similar question when I write the permission of READ_WRITE_EXTERNAL_STORAGE in AndroidManifest.xml

with no permissions showing up in the app by default , people need to switch the toggle button of storage in the app permissions.Then I modify my targetSdkVersion in build.gradle to less than 23(MNC) and other number related with sdkVersion, the app installed with the permissions on.

The other way is to write requestpermission function in the place that you need the permisson. The code is as follow:

 if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==
    PackageManager.PERMISSION_GRANTED) {
    //do the things} else {
    requestPermissions(new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
      AnyNumber);

Because I have less than 15 reputation so I can't vote for the Guillaume Perrot 's answer.Just use this way to show my idea.

Solution 2

I solved add this if check version for Android M

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
                    requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
            }

Solution 3

My answer is based on my tests on M Preview SDK version 2, using an emulator.

If you target MNC preview API level, WRITE_EXTERNAL_STORAGE is not granted by default and will be part of the new dynamic permission API.

You will see the storage permission as a toggle button in the new app permissions menu in device settings, and you can use Activity.requestPermissions to show the popup for that permission.

However if you target api level < MNC, it won't be classified as a dangerous permission, and thus will be granted without a way for the user to disable it (not showing up in permission settings), and you will not be able to compile code using Activity.requestPermissions anyway as the preview SDK enforces minSdkVersion="MNC" to use the new APIs.

This is a different behavior than location permissions: whatever the API level you target, the user will be able to turn location off in permission menu.

For the permission menu itself, the permission toggle state is ON by default if:

  • Target API level < MNC.
  • Target API level = MNC but you upgrade app on device from a previous install where target API level was less than MNC.

Otherwise you will see the toggle as OFF by default.

Hope it helps.

Share:
30,875
Matt
Author by

Matt

Updated on April 09, 2020

Comments

  • Matt
    Matt about 4 years

    Will the Android permissions WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE trigger the new grant permission dialog of Android M?

  • greywolf82
    greywolf82 almost 9 years
    Android docs say for example that the internet permission is granted without problem but it's now dangerous, so there only two hypothesis: the docs is completely wrong or there is a change in Android m source code about the protection level.
  • yshahak
    yshahak almost 9 years
    I think that the docs is not completely clear about this. The only explicit thing is that "the system grants the app all permissions that the app requests that fall under PROTECTION_NORMAL."
  • greywolf82
    greywolf82 almost 9 years
    I just chechek, in android m some permissions has been downgraded, so your answer is completely wrong
  • yshahak
    yshahak almost 9 years
    First of all I said about the read Permmision that it is in normal level, so half is right for sure... Second, where you found data about the permmision level in M?
  • greywolf82
    greywolf82 almost 9 years
    Google is your friend
  • Guillaume Perrot
    Guillaume Perrot over 8 years
    And the difference I observed between storage and location permissions is clearly undocumented... I have no idea if it's by design, a documentation bug or a platform bug.
  • fattire
    fattire over 8 years
    Where do you see that?
  • Jacek Kwiecień
    Jacek Kwiecień over 8 years
    if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STOR‌​AGE)== PackageManager.PERMISSION_GRANTED) is fulfilled even if I revoke permission in the settings. Then the app fail...
  • minipif
    minipif about 8 years
    READ_EXTERNAL_STORAGE isn't normal, it's dangerous.