Flutter Dio在设备上没有下载任何内容,但在模拟器上可以下载



这是我的部分代码:

onPressed: () async {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
].request();
if (statuses[Permission.storage]!.isGranted) {
Directory dir = Directory('/storage/emulated/0/Download');
String savename = "file.gpx";
String savePath = "${dir.path}/$savename";
print(savePath);
try {
await Dio().download(fileurl, savePath,
onReceiveProgress: (received, total) {
if (total != -1) {
print(
"${(received / total * 100).toStringAsFixed(0)}%");
}
});
print("File is saved to download folder.");
} on DioError catch (e) {
print(e.message);
}
// }
} else {
print("No permission to read and write.");
}

它可以在模拟器上正确地下载文件并打印

I/flutter ( 8396): /storage/emulated/0/Download/file.gpx
I/flutter ( 8396): 100%
I/flutter ( 8396): File is saved to download folder.

但是当我在android设备上按下相同的按钮时(在接受许可存储后),下载目录

中没有文件

你应该使用getApplicationDocumentsDirectory()从path_provider包来检测保存下载文件的应用程序目录

最新更新