androidflutterdartpermissionsgallery

Flutter image-picker explicitly ask permission for android version 33 and above or Granular media permissions


Android 33 and above version gallery doesn't open and no request permission.

Permission.storage.request() returns permanentlyDenied when it's allowed on android 13


Solution

  • If your app targets Android 13 or higher and needs to access media files that other apps have created, you must request one or more of the following granular media permissions instead of the READ_EXTERNAL_STORAGE permission:

    https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions

    AndroidManifest:

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

    This pemission only photos and video

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

    pubspec.yaml:

    device_info_plus: ^8.0.0
    

    clean and get dependencies to avoid crash your application

    flutter clean && flutter pub get
    

    Wherever your code is:

    bool isStoragePermission = true;
    bool isVideosPermission = true;
    bool isPhotosPermission = true;
    
    // Only check for storage < Android 13
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    if (androidInfo.version.sdkInt >= 33) {
      isVideosPermission = await Permission.videos.status.isGranted;
      isPhotosPermission = await Permission.photos.status.isGranted;
    } else {
      isStoragePermission = await Permission.storage.status.isGranted;
    }
    
    if (isStoragePermission && isVideosPermission && isPhotosPermission) {
      // no worries about crash 
    } else {
      // write your code here
    }