1

I'm trying to build a Flutter application that runs on Android 11 and downloads files. I used to manage external storage permission to achieve this, but when the application asks for permission it goes to settings directly instead of asking for allow or deny within the app.

For example, WhatsApp stores data in the android/media folder, but it asks for permission directly within the application instead of going to the settings page. Please refer the below images:

My application goes to settings like this / I need something like this

My permission handling code

Future<bool> requestPermission() async {
  var androidInfo = await DeviceInfoPlugin().androidInfo;
  var release = int.parse(androidInfo.version.release);
  Permission permission;
  if (release < 11) {
    permission = Permission.storage;
  } else {
    permission = Permission.manageExternalStorage;
  }
  if (await permission.isGranted) {
    return true;
  } else {
    var result = await permission.request();
    if (result == PermissionStatus.granted) {
      return true;
    } else {
      return false;
    }
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Narayana
  • 19
  • 1
  • 4

2 Answers2

0

These are two different approaches to manage the access the external storage in the Android device for Android 11 and above.

Files Go is trying to access the all files access permission. This gives app to access any read, write, access and share all files present in user storage(external or internal). You can read more about all files access permission from here - Manage all files on a storage device

WhatsApp - instead of asking for all files access permission, is saving its media in files directory. Saving in the files or cache directory doesn't need access to all flies and a simple permission dialog to write and read storage does the task.

You can read more about it in getExternalFilesDir.

Note: This only applies if your targetSdkVersion is 30. Targeting a 29 or below version, there isn't any need to all files permission as it's handled by the system.

You can refer to storage updates and request permission according to your use case in Storage updates in Android 11.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nitish
  • 3,075
  • 3
  • 13
  • 28
  • The links you provided didn't mention how to implement it in flutter like whatsapp – Narayana Sep 13 '21 at 09:29
  • Remove Permission.manageExternalStorage from the code should do the trick – Nitish Sep 13 '21 at 09:41
  • if you remove Permission.manageExternalStorage you can no longer read hidden folder for this location /storage/emulated/0/Android/Media/com.whatsapp/WhatsApp/Media/.Statuses Any Solution to this ? – Ankit Parmar Nov 01 '21 at 09:08
  • @AnkitParmar , if it's your app package name/storage you can read it , if it's not your app you can't access it. – Nitish Nov 02 '21 at 04:37
  • @Nitish Ya I know but how to read other apps hidden folder ? without Permission.manageExternalStorage permission ? – Ankit Parmar Nov 02 '21 at 08:08
  • @AnkitParmar no you can't. And as far as I remember you can't read hidden folder when targeting Android 11 or above even with the permission – Nitish Nov 02 '21 at 09:54
  • [hidden folder Android 11](https://stackoverflow.com/questions/68528317/unable-to-get-files-from-folder-with-nomedia-file-or-hidden-folder-in-android-1) , [access other apps data](https://stackoverflow.com/questions/67556084/android-11-access-other-apps-data-in-android-media) , [açcess hidden file](https://stackoverflow.com/questions/69632521/is-there-any-way-to-access-hidden-files-android), refer to these links , they are more related to your query , none of these have answers, but have comments which might be helpful to you – Nitish Nov 02 '21 at 09:59
  • @Nitish Ya got your point but when i uploaded the app with added permission like "Permission.manageExternalStorage" my app got rejected due to policy issue but other whatsapp saver are getting files using such permission https://i.stack.imgur.com/eDurs.jpg as shown in this image and google is allowing them how any why ? Even my device is running on android 11 still i get the same permission pop up shown in this image https://i.stack.imgur.com/eDurs.jpg then why google is allowing third party WhatsApp saver app like this ? – Ankit Parmar Nov 02 '21 at 19:22
  • need to understand how we can create folders just like whatsApp in /Storage/emulated/0/Andorid/media/com.whatsApp/WhatsApp/Media .. and the same is visible on /storage/emulated/0/WhatsApp how is that .. and do we have any code Snipped which can help to Code. – Vrajendra Singh Mandloi Feb 07 '22 at 10:47
  • @VrajendraSinghMandloi - Follow this answer [how-to-create-folder-inside-android-media-in-android-11](https://stackoverflow.com/questions/69658332/how-to-create-folder-inside-android-media-in-android-11) - it doesn't have any answer but have some useful comments. – Nitish Feb 07 '22 at 11:39
  • @Nitish, I am trying to achieve the same /storage/emulated/0/Android/Media/com.myAp/Media/... folders corresponding to my app. need to understand how to achieve via Flutter. is their any code Snipped which would be really helpful. directly in flutter is there any way or we need to call via kotlin.. – Vrajendra Singh Mandloi Feb 07 '22 at 13:02
  • @VrajendraSinghMandloi , sorry I haven't tried so I don't have any code snippet, but there should be a way using flutter. – Nitish Feb 07 '22 at 13:18
0

Even if you are in Android 11 or above we have to ask the storage permission not the manage external storage permission in Flutter. I tried to write the files to 'android/media/packagename/' and it is working normally, but when I try to write into other locations it's not working. I think somehow Android internally is allowing to store data only in the same package.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Narayana
  • 19
  • 1
  • 4