【问题标题】:Flutter cant save to android application directoryFlutter无法保存到android应用程序目录
【发布时间】: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


【解决方案1】:

你需要复制我认为的文件。看看下面的代码

//File created.
    File tmpFile = File(imageFile.path);

    final appDir = await getApplicationDocumentsDirectory();
//filename - returns last part after the separator - path package.
    final fileName = basename(imageFile.path);
//copy the file to the specified directory and return File instance.
    tmpFile = await tmpFile.copy('${appDirPath.path}/images/profileImage$fileExt');

【讨论】:

  • 感谢您的建议,这几乎就是我的代码现在所做的。虽然希望我在做一些愚蠢和错误的事情,但我尝试了这个,果然我得到了同样的错误。无法将文件复制到(操作系统:错误:没有这样的文件或目录,errno = 2
  • 文件不存在或您尝试访问的目录不存在。在AS中使用设备文件资源管理器并检查
  • 没有更多代码或错误堆栈,我们无法帮助您
【解决方案2】:

因此,经过一些广泛的谷歌搜索和反复试验,我最终制定了一些较小的方法来让一切正常工作。

首先,我创建一个目录来检查它是否已经存在,然后添加我要保存的文件夹名称。

  // Checked and working
  Future<String> createFolderInAppDocDir(String folderName) async {
    //Get this App Document Directory

    final Directory _appDocDir = await getApplicationDocumentsDirectory();
    //App Document Directory + folder name
    final Directory _appDocDirFolder = Directory('${_appDocDir.path}/$folderName');

    final Directory _appDocDirNewFolder = await _appDocDirFolder.create(recursive: true);
    return _appDocDirNewFolder.path;
  }

然后我做了另一种方法,根据我保存的文件类型来实现保存到该位置...

  Future<File?> saveFileLocally(String type, File _file) async {
    File tmp;
    try {
      // Create or get Folder location
      final appDir = await createFolderInAppDocDir(type);

      // Get File Extension
      String fileExt = extension(_file.path);

      // Copy file to new location
      tmp = await _file.copy('$appDir/${Timestamp.now().millisecondsSinceEpoch}$fileExt');

      // Delete cached file
      final bool tmpExists = await tmp.exists();
      if (tmpExists) _file.delete();

      //Utilities.logWarning('New path is: ${tmp.path}');
      return tmp;
    } catch (e) {
      Utilities.logError('Error: $e');
      return null;
    }
  }

Utilities.logError 只是一个调试语句颜色代码,因此我可以更轻松地找到它们。现在,它就像我需要保存的任何东西的魅力一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多