5

I'm making the use of read and write permission for accessing external storage and for getting the permission, I'm using the permission handler package

AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

For getting the permission from user

var status = await Permission.storage.request();
if (status.isGranted) {
  Navigator.of(context).pushReplacement(
    MaterialPageRoute(builder: (context) => const Home())
  );
} else if (status.isPermanentlyDenied) {
  openAppSettings();
}

The above permissions and code working perfectly for android versions till 12 but when it comes to android version 13, it is not working up, it just opens up the app setting instead of asking the permission

william xyz
  • 710
  • 5
  • 19
shakti goyal
  • 161
  • 1
  • 1
  • 7
  • 1
    This happens because the permission is already released, there was a change in this permission in relation to sdk 33, as [described here](https://github.com/Baseflow/flutter-permission-handler/issues/885) and [here in SO](https://stackoverflow.com/questions/72948052/android-13-read-external-storage-permission-still-usable). – Chance Jan 31 '23 at 14:45
  • So you are suggesting that I don't need to ask for the storage permission in android versions which are greater than 12? What I'm doing is getting the video url from API and saving it to the gallery, do I need permission for that? @Chance – shakti goyal Jan 31 '23 at 14:53
  • Pretty strange you are still asking as you know already that permissions are given implicit. And why not just test it? And you did not tell which storage location you are after. – blackapps Jan 31 '23 at 14:59
  • Just tested it! No need to ask permission in android version 13 :) @blackapps – shakti goyal Jan 31 '23 at 15:19
  • You should check the permission yes, but in the permission handler there is a bug where it checks the permission in sdk 33 with denied status, as [described in their issue](https://github.com/Baseflow/flutter-permission-handler/issues/885). – Chance Jan 31 '23 at 15:39

5 Answers5

6

For Android 13 or higher

Menifest Permissions

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>

permission_handler (request)

await Permission.photos.request();
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Usama
  • 61
  • 1
6

setp 1

add permission android manifest

<!-- Required only if your app targets Android 13 or higher. -->
<uses-permission android:name="com.google.android.gms.permission.AD_ID"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
     android:maxSdkVersion="33" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
       android:maxSdkVersion="33"  />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>

setp 2

var status = await Permission.manageExternalStorage.request();
if (status.isGranted) {
  Navigator.of(context).pushReplacement(
    MaterialPageRoute(builder: (context) => const Home())
  );
} else if (status.isPermanentlyDenied) {
  openAppSettings();
}

enter image description here

Pradip D.
  • 225
  • 3
  • 14
5

If you use targetSdk version 33 then use following code.

bool permissionStatus;
final deviceInfo = await DeviceInfoPlugin().androidInfo;
    
if (deviceInfo.version.sdkInt > 32) {
    permissionStatus = await Permission.photos.request().isGranted;
} else {
    permissionStatus = await Permission.storage.request().isGranted;
}

use this permission manifest file:

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
william xyz
  • 710
  • 5
  • 19
Anik Roy
  • 51
  • 1
  • 2
0

Add this code in AndroidMenifest.xml for permission,

<!--for android 13 and above-->
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" /> <!-- For Images-->
    <uses-permission android:name="android.permission.READ_MEDIA_VIDEOS" /> <!-- For video-->
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" /> <!-- For Audio-->

<!-- for below android 13-->
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Add this dependencies in pubspec.yaml

device_info_plus: ^version

Add this snippet of code :

Future<void> _getStoragePermission() async {
    DeviceInfoPlugin plugin = DeviceInfoPlugin();
    AndroidDeviceInfo android = await plugin.androidInfo;
       if (android.version.sdkInt < 33) {
          if (await Permission.storage.request().isGranted) {
             setState(() {
               permissionGranted = true;
             });
          } else if (await Permission.storage.request().isPermanentlyDenied) {
             await openAppSettings();
          } else if (await Permission.audio.request().isDenied) {
             setState(() {
               permissionGranted = false;
             });
          }
        } else {
           if (await Permission.photos.request().isGranted) {
                setState(() {
                   permissionGranted = true;
                });
           } else if (await Permission.photos.request().isPermanentlyDenied) {
               await openAppSettings();
           } else if (await Permission.photos.request().isDenied) {
               setState(() {
                   permissionGranted = false;
               });
         }
      }
}

Happy Codding!

Pradip D.
  • 225
  • 3
  • 14
0

In a recent developer release, there is no need to ask for permission in case of files, so you can directly use FilePicker to access storage, But you need to specify the follwoing permissions in the Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

And the permission handler_package is buggy for this, which always return denied as the status

Noob Master
  • 91
  • 1
  • 10