我创建了一个在我的外部目录中创建文件夹的函数。之后,我从互联网上下载一张图片并将其保存到“图库图片”文件夹中。图像正在保存在文件夹中,但在厨房中不可见。我错过了什么吗?
代码:
void createFolder() async {
String directory = (await p.getExternalStorageDirectory()).path;
List<String> externalPathList = directory.split('/');
int posOfAndroidDir = externalPathList.indexOf('Android');
String rootPath = externalPathList.sublist(0, posOfAndroidDir + 1).join('/');
final path = Directory("$rootPath/Gallery");
var storageStatus = await Permission.storage.status;
var externalStorageStatus = await Permission.manageExternalStorage.status;
if (!storageStatus.isGranted) {
await Permission.storage.request();
}
if (!externalStorageStatus.isGranted) {
await Permission.manageExternalStorage.request();
}
if ((await path.exists())) {
print("exists");
print(path.path.toString());
} else {
var value = await path.create();
print("create success");
print(value.path.toString());
await Directory(rootPath + "/Gallery" + "/Gallery Images").create(recursive: true);
await Directory(rootPath + "/Gallery" + "/Gallery Video").create(recursive: true);
await Directory(rootPath + "/Gallery" + "/Gallery Documents").create(recursive: true);
await Directory(rootPath + "/Gallery" + "/Gallery Audio").create(recursive: true);
await Directory(rootPath + "/Gallery" + "/Gallery Voice Notes").create(recursive: true);
await Directory(rootPath + "/Gallery" + "/.thumbnails").create(recursive: true);
await Dio().download(url, rootPath + "/Gallery" + "/Gallery Images", onReceiveProgress: (int sent, int total) {
final progress = (sent / total) * 100;
print('image download progress: $progress');
});
}
}
最佳答案
每当您创建新文件时,设备都无法自动找到它。您必须手动告诉设备刷新文件。之前你必须刷新设备中的所有文件才能更新,这是非常低效的,但现在你可以只发送你想要更新的文件的路径。如果您使用的是 android,那么您可以使用这个 package这样做。
如果你想用 kotlin 自己做,那么这里是代码
private fun broadcastFileUpdate(path: String) {
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(File(path))))
println("updated!")
}
关于flutter - 保存的图像不会在图库中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69016968/