Flutter[Android]:无法使用管理外部存储权限发布应用程序

14
我们需要允许用户将文件存储在外部存储器中,为此,在我们的应用程序中使用“MANAGE_EXTERNAL_STORAGE”权限。
理想情况下,对于Android SDK版本30及以上,我们使用“Permission.manageExternalStorage”,对于低于30的Android SDK版本,我们使用“Permission.storage”,如下所示的代码。
  // 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;
    }
  }

使用以上实现方案,在开发和内部发布过程中一切都运行良好,但是 Google Play 控制台拒绝了该应用并给出了以下拒绝原因(我们已经提交了 manage_storage 权限的原因)。 enter image description here

现在在Android 13上,您需要根据要检索的文件添加新的权限来访问存储:https://developer.android.com/about/versions/13/behavior-changes-13#granular-media-permissions - T E
2个回答

0

我在我的项目中找到并应用了以下解决方案来解决上述问题。

  1. 我们必须代表存储权限shared_storage使用SAF(存储访问框架),这将允许我们授予共享存储中特定目录的权限,并且我们可以将文件存储在那里。我还添加了相同的代码示例,如下所示。
  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;
    }
  }

-1
我们需要允许用户将文件存储在外部存储器中,为此,在我们的应用程序中使用MANAGE_EXTERNAL_STORAGE权限。
您不需要该权限来在公共目录(如Download、Documents、DCIM、Pictures等)中创建自己的子目录。
此外,您也不需要该权限来在这些目录和子目录中创建文件。

我同意在范围存储中创建新目录时不需要 MANAGE_EXTERNAL_STORAGE 权限,但是在 Flutter 中有什么方法可以创建目录并将文件保存在那里呢? - Dhaval Kansara
您想在公共外部存储中创建文件。与在内部或应用程序特定的外部存储中创建文件的方式相同,只是使用的路径不同。请使用正确的路径。 - blackapps
实际上,除非您的应用程序属于以下类别,否则请勿使用此权限,否则Play控制台将拒绝您的应用程序。 文件管理器, 备份和恢复应用程序, 反病毒应用程序, 文档管理应用程序, 设备上的文件搜索, 磁盘和文件加密, 设备到设备数据迁移 - Mehran Ullah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接