[英]how to download https network URL images into app directory in flutter
將 'package: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);
}
它看起來類似於: 如何從在線下載視頻並將其存儲到本地設備,然后使用視頻播放器在 Flutter 應用程序上播放視頻?
這是我當時給出的答案:
您可能想嘗試一下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_provider tu 使用getApplicationDocumentsDirectory()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.