【问题标题】:path directory in flutter using path_provider使用 path_provider 在颤振中的路径目录
【发布时间】:2021-11-07 00:34:28
【问题描述】:

在我的应用程序中,我正在从服务器下载文件。我需要将此文件保存到手机存储中的下载文件夹中。这可以在android中使用path_provider包吗?

【问题讨论】:

    标签: flutter dart download storage


    【解决方案1】:

    这可能与this question 重复。

    查看此answer.

    您可能需要考虑将文件保存在应用的应用目录中,如path_provider. 的官方 pub.dev 文档中所述

    【讨论】:

      【解决方案2】:

      您可以使用Dio 进行下载,使用downloads_path_provider_28 集体获取下载文件夹路径:

      Future download(String url) async {
        final Dio dio = Dio();
        Directory? downloadsDirectory = await DownloadsPathProvider.downloadsDirectory; // "/storage/emulated/0/Download"
        final savePath = downloadsDirectory?.path;
        try {
          Response response = await dio.get(
            url,
            onReceiveProgress: (received, total) {
              if (total != -1) {
                print((received / total * 100).toStringAsFixed(0) + "%");
              }
            },
            options: Options(
              responseType: ResponseType.bytes,
              followRedirects: false,
              validateStatus: (status) {
                return status < 500;
              }
            ),
          );
          print(response.headers);
          File file = File(savePath);
          var raf = file.openSync(mode: FileMode.write);
          // response.data is List<int> type
          raf.writeFromSync(response.data);
          await raf.close();
        } catch (e) {
          print(e);
        }
      }
      
      

      【讨论】:

      • ext_storage 不是空安全包。我们如何在 path_provider 包中制作它?
      • 无法从 path_provider 获取此下载目录。而且我已经将使用的库更新为空安全库。
      猜你喜欢
      • 2022-01-12
      • 2021-08-13
      • 2020-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多