Flutter[Android]: Not able to release App with Manage External Storage Permission

103

I found and applied the below solution for the above issue in my project.

  1. We have to use SAF(Storage Access Framework) on behalf of the storage permission shared_storage which will allow us to grant the permission for the specific directory in the shared storage and we can store the files over there. I have also added the code sample for the same below.
  Future<void> exportFile({
    required String csvData,
    required String fileName,
  }) async {
    Uri? selectedUriDir;
    final pref = await SharedPreferences.getInstance();
    final scopeStoragePersistUrl = pref.getString('scopeStoragePersistUrl');
   
    // Check User has already grant permission to any directory or not
    if (scopeStoragePersistUrl != null &&
        await isPersistedUri(Uri.parse(scopeStoragePersistUrl)) &&
        (await exists(Uri.parse(scopeStoragePersistUrl)) ?? false)) {
      selectedUriDir = Uri.parse(scopeStoragePersistUrl);
    } else {
      selectedUriDir = await openDocumentTree();
      await pref.setString('scopeStoragePersistUrl', selectedUriDir.toString());
    }
    if (selectedUriDir == null) {
      return false;
    }
    try {
      final existingFile = await findFile(selectedUriDir, fileName);
      if (existingFile != null && existingFile.isFile) {
        debugPrint("Found existing file ${existingFile.uri}");
        await delete(existingFile.uri);
      }
      final newDocumentFile = await createFileAsString(
        selectedUriDir,
        mimeType: AppConstants.csvMimeTypeWhileExport,
        content: csvData,
        displayName: fileName,
      );
      return newDocumentFile != null;
    } catch (e) {
      debugPrint("Exception while create new file: ${e.toString()}");
      return false;
    }
  }
Share:
103
Dhaval Kansara
Author by

Dhaval Kansara

iOS | Flutter Developer

Updated on January 04, 2023

Comments

  • Dhaval Kansara
    Dhaval Kansara over 1 year

    We need to allow users to store files in the external storage and for the same, we use MANAGE_EXTERNAL_STORAGE permission in our application.

    Ideally for the android SDK version 30 and above we are using Permission.manageExternalStorage and using Permission.storage for android SDK versions lower than 30 as shown in the below code

      // This func is added to access scope storage to export csv files
      static Future<bool> externalStoragePermission(BuildContext context) async {
        final androidVersion = await DeviceInfoPlugin().androidInfo;
    
        if ((androidVersion.version.sdkInt ?? 0) >= 30) {
          return await checkManageStoragePermission(context);
        } else {
          return await checkStoragePermission(context);
        }
      }
    
      static Future<bool> checkManageStoragePermission(BuildContext context) async {
        return (await Permission.manageExternalStorage.isGranted ||
            await Permission.manageExternalStorage.request().isGranted);
      }
    
      static Future<bool> checkStoragePermission(BuildContext context,
          {String? storageTitle, String? storageSubMessage}) async {
        if (await Permission.storage.isGranted ||
            await Permission.storage.request().isGranted) {
          return true;
        } else {
          openBottomSheet(
            title: storageTitle ?? Str.of(context).storagePermissionRequired,
            message: storageSubMessage ?? Str.of(context).storageSubMessage,
          ).show(context);
          return false;
        }
      }
    

    With the above implementation, everything worked fine while the development and internal release but the Google play console reject the application with the below rejections(Also we have submitted the reason as well for manage_storage permission).

    enter image description here

  • Dhaval Kansara
    Dhaval Kansara about 2 years
    I agreed I don't need MANAGE_EXTERNAL_STORAGE permission to create the new directory in scope storage but what is the way using which I can Create dir and save file over there in flutter?
  • blackapps
    blackapps about 2 years
    You want to create files in public external storage. You do that the same way as when you create files in internal or app specific external storage. Only the used path is different. Take the right path.