flutterflutter-image-picker

How to request the user to access the storage of phone?


enter image description here

I try to code some gallery picker function from Youtube but I want to ask for permission for storage to user ,when I code below code, I get into the app info why I get there and how to fix it

import 'package:photo_manager/photo_manager.dart';

class MediaServices {
  Future loadAlbum(RequestType requestType) async {
    var permission = await PhotoManager.requestPermissionExtend();
    List<AssetPathEntity> albumList = [];
    if (permission == true) {
      albumList = await PhotoManager.getAssetPathList(
        type: requestType,
      );
    } else {
      PhotoManager.openSetting();
    }
    return albumList;
  }
  Future loadAssets(AssetPathEntity selectedAlbum) async {
  // ignore: deprecated_member_use
  List<AssetEntity> assetList = await selectedAlbum.getAssetListRange(
      // ignore: deprecated_member_use
      start: 0, end: selectedAlbum.assetCount);
      return assetList;
}

}

Solution

  • Use Permission handler plugin from pub.dev, this is similar answer

    for android add these lines in Androidmainfest.xml file

    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permissionandroid:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
    

    for iOS add below code in info.plist file

    <key>NSMicrophoneUsageDescription</key>
    <string>This app require permission to access microphone</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>This app require permission to access gallery</string>
    

    modify below code accordingly.

     Future<void> getStorageReadPermission() async {
        final storage = await Permission.storage.status;
        !storage.isGranted
            ? await Permission.storage.request()
            : debugPrint("Storage read permission already granted");
      }
    
      Future<void> getStorageManagePermission() async {
        final manageStorage = await Permission.manageExternalStorage.status;
        !manageStorage.isGranted
            ? await Permission.manageExternalStorage.request()
            : debugPrint("storage permission already granted");
      }