flutter - 如何在flutter中将https网络URL图像下载到应用程序目录中
问题描述
将“包:http/http.dart”导入为 http;
导入“包: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);
}
解决方案
这是我当时给出的答案:
您可能想尝试一下dio包,它是一个支持文件下载并将其本地保存到给定路径的 http 客户端。
这是一个代码示例(来源:iampawan 的 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_providergetApplicationDocumentsDirectory()
。
推荐阅读
- python - Elasticsearch 的 Python 自动批量请求不起作用“必须由换行符终止”
- r - 为特定行重复编号的新列 R
- php - 时间顺序列表
- grep - 用星号中的 grep 过滤 pjsip 显示频道
- python - 类型错误:__init__() 缺少 1 个必需的位置参数:slate3k 中的“解析器”
- c# - 基于相同的密钥在 Kafka 中加入/分组多个主题
- python - 如何定义folium颜色图位置
- java - 在 java spring hibernate 3.6.3 中,如何告诉方法等到锁被获取以及如何重试失败的事务?
- javascript - 如果我按下 Windows aka 开始按钮,如何暂停视频,就像打开新选项卡时此代码如何暂停视频一样?
- html - 如何使用 Angular 6 在 mat-select 中获取下拉文本