Flutter无法创建目录(操作系统错误:只读文件系统)

时间:2018-07-31 06:47:09

标签: flutter

我一直在关注-如何从https://docs.flutter.io/flutter/dart-io/Directory-class.html创建目录

new Directory('dir/subdir').create(recursive: true)
  // The created directory is returned as a Future.
  .then((Directory directory) {
    print(directory.path);
});

这是我得到的错误:

  

FileSystemException:创建失败,路径='dir'(操作系统错误:只读文件系统,errno = 30)

我已启用Storage(WRITE_EXTERNAL_STORAGE)权限。 (Android设备)

我想念什么?

3 个答案:

答案 0 :(得分:3)

知道了(使用path_provider插件获取正确的路径)

Directory appDocDirectory = await getApplicationDocumentsDirectory();

new Directory(appDocDirectory.path+'/'+'dir').create(recursive: true)
// The created directory is returned as a Future.
    .then((Directory directory) {
  print('Path of New Dir: '+directory.path);
});

答案 1 :(得分:0)

如果您正在Android Oreo上进行测试,并且想在外部存储中写入文件,则需要询问WRITE_EXTERNAL_STORAGE

根据“ https://developer.android.com/about/versions/oreo/android-8.0-changes.html#rmp

  

如果该应用定位到Android 8.0(API级别26),则系统仅授予   当时是READ_EXTERNAL_STORAGE;但是,如果应用程序稍后请求   WRITE_EXTERNAL_STORAGE,系统立即授予该特权   而不提示用户。

Permission Plugin仅向Permission.requestPermissions([PermissionName.Storage])申请READ_EXTERNAL_STORAGE权限; 所以您需要索取WRITE_EXTERNAL_STORAGE。

您可以使用我的存储库在pubspec.yaml中询问WRITE_EXTERNAL_STORAGE:

permission:
    git:
    url: https://github.com/kamlesh9070/permission

答案 2 :(得分:0)

如果您授予了WRITE_EXTERNAL_STORAGE权限,但仍然无法创建目录,

您可以尝试从设备上手动卸载 Flutter应用,

然后重新运行。

注意:确保在安装后,从应用设置中允许存储权限

经过数小时的努力,它对我有用(我不知道为什么该解决方案有效)

相关问题