gpt4 book ai didi

image - Flutter 如何移动文件

转载 作者:IT老高 更新时间:2023-10-28 12:44:52 39 4
gpt4 key购买 nike

我将移动某些文件。如何将图像文件从目录移动到另一个目录,示例文件 img.jpg 从/storage/emulated/0/Myfolder 到/storage/emulated/0/Urfolder?

最佳答案

File.rename 仅在源文件和目标路径位于同一文件系统上时才有效,否则您将收到 FileSystemException (OS Error: Cross-设备链接,errno = 18)。因此,仅当您确定源文件和目标路径位于同一文件系统上时,才应使用它来移动文件。

例如,当尝试将文件夹 /storage/emulated/0/Android/data/ 下的文件移动到文件夹 /data/user/0/com 下的新路径时.my.app/cache/ 将失败 FileSystemException

这是一个用于安全移动文件的小实用函数:

Future<File> moveFile(File sourceFile, String newPath) async {
try {
// prefer using rename as it is probably faster
return await sourceFile.rename(newPath);
} on FileSystemException catch (e) {
// if rename fails, copy the source file and then delete it
final newFile = await sourceFile.copy(newPath);
await sourceFile.delete();
return newFile;
}
}

关于image - Flutter 如何移动文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54692763/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com