Flutter - Android 13 How to handle storage permission

We know that google, every year releases a new android update that comes with new changes in the android version. Now android 13 is the latest version of Android and starting roll-out in many devices. So we need to take care of new changes in android 13, So our app function will not break in that devices. In android 13 one of the major changes is in storage permission, Sometimes we need to access the user's media files like photos, videos, audio, etc but without storage permission, we can't access these. Before android 13 we need only a read storage permission and now onward android 13 we need to handle new Granular media permissions which are new in android 13. So now In this article, I am sharing with you in flutter Android 13 How to handle storage permission.

 

How to handle storage permission In Android 13.

The first step is we need to to add the latest package version of  permission_handler and device_info_plus our project's pubspec.yaml.

dependencies:
  permission_handler: ^10.2.0
  device_info_plus: ^8.0.0

Here we used device_info_plus package for checking the android version. The second step is we need to check permission programmatically, Like below.

 

Future<bool> checkStoragePermission() async {
    if (Platform.isAndroid) {
      final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
      final AndroidDeviceInfo info = await deviceInfoPlugin.androidInfo;
      if ((info.version.sdkInt ?? 0) >= 33) return true;
    }
    Permission permission = Permission.storage;
    PermissionStatus permissionStatus = await permission.status;
    if (permissionStatus == PermissionStatus.denied) {
      permissionStatus = await permission.request();
      if (permissionStatus == PermissionStatus.permanentlyDenied) {
        redirectToSetting(Strings.msgPermissionStorage);
        return false;
      }
      if (permissionStatus == PermissionStatus.denied) {
        permissionStatus = await permission.request();
        return false;
      }
    } else if (permissionStatus == PermissionStatus.granted) {
      return true;
    } else if (permissionStatus == PermissionStatus.permanentlyDenied) {
      //openAppSettings();
      redirectToSetting(Strings.msgPermissionStorage);
      return false;
    }
  }

 

Conclusion:

In this post, we saw Android 13 How to handle storage permission in a flutter. I hope this post may help you.

 

Tags: