【发布时间】:2022-06-19 22:26:15
【问题描述】:
我已经搜索了互联网,并且正在做每个人都说要做的事情,但由于某种原因,我无法将文件图像或其他方式保存到应用程序文件夹中。
我有这个方法来获取应用程序目录...
Future<Directory> get _localAppPath async {
Directory? directory;
if (Platform.isAndroid) {
directory = await getApplicationDocumentsDirectory();
Utilities.logInfo('Local Android App Path is: ${directory.path}');
} else {
// if IOS Device
directory = await getTemporaryDirectory();
Utilities.logInfo('Local IOS App Path is: $directory');
}
return directory;
}
我在我的保存方法中使用它......
Future<void> saveProfileImageLocally(File _file) async {
try {
final appDirPath = await _localAppPath;
//Utilities.logWarning('New path is: ${appDirPath.path}');
final fileExt = extension(_file.path);
// Check is directory exists
Utilities.logWarning('FilePath: ${_file.path}');
File newFile = await _file.rename('${appDirPath.path}/images/profileImage$fileExt');
Utilities.logWarning('New path is: ${newFile.path}');
Storage.saveValue('profileImage', newFile.path);
} catch (e) {
Utilities.logError(e.toString());
}
}
我会在每次应用启动时检查权限,因此我知道我有权限 但无论我不断收到这个错误,没有这样的文件或目录...... 在阅读另一篇 stackoverflow 帖子之前,我一直在尝试使用复制功能。
FileSystemException: Cannot rename file to '/data/user/0/ca.company.example/app_flutter/images/profileImage.jpg', path = '/data/user/0/ca.company/example/cache/CAP370489784397780451.jpg' (OS Error: No such file or directory, errno = 2)
这应该是我一直在线阅读的所有资源和教程中的一个简单的单行过程...所以我很困惑我错过了哪一步。
任何帮助将不胜感激。
【问题讨论】:
-
你试过
await _file.copy()而不是rename()吗? -
是的,一开始我大部分时间都在尝试复制,我最近才看到有人使用重命名作为它应该创建的论坛,如果我不理解的话
标签: android flutter file dart directory