【问题标题】:Download file to Device download folder in Flutter将文件下载到 Flutter 中的 Device 下载文件夹
【发布时间】:2020-04-24 09:12:13
【问题描述】:

我正在尝试使用必须指定“savePath”的 dio 插件在我的颤振应用程序中下载文件。但是使用 path_provider 我只能看到 ApplicationDocumentsDirectory、ExternalStorageDirectory 等。我的想法是在设备的下载文件夹中简单地显示文件(pdf),所以每当用户想要查看文件时,他只需转到该文件夹​​并单击它。 保存到 ApplicationDocumentsDirectory(据我所知)需要我提供指向应用程序中文件的链接(各种),这意味着用户每次想要查看通过我的应用程序下载的文件时都必须打开我的应用程序。

我只希望文件在 DownLoads 文件夹中可用,就像我们直接下载到 DownLoads 文件夹中的许多文件一样。

我在这里遗漏了什么吗?请赐教!

根据用户要求在 cmets 中添加此代码:

var dir;
Future < dynamic > downloadFile(url, filename) async {
  Dio dio = Dio();

  try {
    //  checkPermission();
    //if(Externa)
    dir = await getApplicationDocumentsDirectory();
    if (dir.path != null)
      print("url:$url");
    print("path:${dir.path}/$filename");
    if (await File("${dir.path}/$filename").exists()) {
      setState(() {

        filePath = "${dir.path}/$filename";
      });
      return;
    }
    await dio.download(url, "${dir.path}/$filename",
      onReceiveProgress: (rec, total) {
        print("Rec: $rec , Total: $total");

        setState(() {
          downloading = true;
          progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
        });
      });
  } catch (e) {
    print("downloadErr:$e");
  }

  setState(() {
    downloading = false;
    progressString = "Concluido";
    filePath = "${dir.path}/$filename";

  });

}

【问题讨论】:

  • 您好,我也遇到了和您一样的问题,请问您找到解决方法了吗?我尝试过使用downloads_path_provider,但是在我看到下面的 cmets 之后......这个包不再工作了
  • @uyhaW,我在原始问题中添加了一些代码。看看它是否适合你。

标签: flutter dart-io


【解决方案1】:

您可以使用 downloads_path_provider

https://pub.dev/packages/downloads_path_provider

import 'package:downloads_path_provider/downloads_path_provider.dart';  

Future<Directory> downloadsDirectory = DownloadsPathProvider.downloadsDirectory;

【讨论】:

  • 这在您签署 apk 时不起作用。它仅在开发过程中有效,因为 compileSdkVersion 为 27。尝试分叉并更改它,但看起来它不起作用。不知道缺少什么。
  • package:downloads_path_provider 被用户放弃并存档,不再工作
猜你喜欢
  • 2020-09-09
  • 2021-05-28
  • 2019-02-20
  • 2012-07-23
  • 1970-01-01
  • 2014-02-17
  • 2011-08-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多