【发布时间】:2021-11-07 00:34:28
【问题描述】:
在我的应用程序中,我正在从服务器下载文件。我需要将此文件保存到手机存储中的下载文件夹中。这可以在android中使用path_provider包吗?
【问题讨论】:
标签: flutter dart download storage
在我的应用程序中,我正在从服务器下载文件。我需要将此文件保存到手机存储中的下载文件夹中。这可以在android中使用path_provider包吗?
【问题讨论】:
标签: flutter dart download storage
【讨论】:
您可以使用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);
}
}
【讨论】: