【问题标题】:how to download https network URL images into app directory in flutter如何在flutter中将https网络URL图像下载到应用程序目录中
【发布时间】:2021-02-10 16:51:21
【问题描述】:

将'package:http/http.dart'导入为http;

导入'package:image_picker_saver/image_picker_saver.dart';

try {
  var response = await http.get(imageURL); // problem is here (only working on http URL)

  debugPrint(response.statusCode.toString());

  var filePath = await ImagePickerSaver.saveFile(
      fileData: response.bodyBytes);

  var savedFile= File.fromUri(Uri.file(filePath));

  print('Image path: $savedFile');

 /* setState(() {
    _imageFile = Future<File>.sync(() => savedFile);
  });*/

} on PlatformException catch (error) {
  print(error);
}

【问题讨论】:

    标签: flutter https dart-pub


    【解决方案1】:

    看起来类似于:How to Download Video from Online and store it local Device then play video on Flutter apps Using video player?

    这是我当时给出的答案:

    你可能想试试dio 包,它是一个支持文件下载并将其本地保存到给定路径的http客户端。

    这是一个代码示例(来源:iampawan's Github

    Future downloadFile(String url) async {
      Dio dio = Dio();
    
      try {
        var dir = await getApplicationDocumentsDirectory();
        await dio.download(url, "${dir.path}/myFile.txt", onProgress: (rec, total) {
          print("Rec: $rec , Total: $total");
        });
      } catch (e) {
        print(e);
      }
      print("Download completed");
    }
    

    您还需要path_provider,请使用getApplicationDocumentsDirectory()

    【讨论】:

      猜你喜欢
      • 2021-10-05
      • 2011-07-02
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 2022-12-09
      • 1970-01-01
      相关资源
      最近更新 更多