我创建了一个在外部目录中创建文件夹的函数。之后,我从互联网下载图像并将其保存到“图库图像”文件夹中。图像正在保存在文件夹中,但在厨房中不可见。我错过了什么吗?
Code:
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');
});
}
}
每当您创建新文件时,设备都无法自动找到它。您必须手动告诉设备刷新文件。以前您必须刷新设备中的所有文件才能更新,这是非常低效的,但现在您只需发送要更新的文件的路径即可。如果你使用的是安卓那么你可以使用这个package https://pub.dev/packages/media_scanner这样做。
如果你想用 kotlin 自己做,那么这里是代码
private fun broadcastFileUpdate(path: String) {
context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(File(path))))
println("updated!")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)