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

Upa*_*Jah 5 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);
});
Run Code Online (Sandbox Code Playgroud)

这是我得到的错误:

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

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

我想念什么?

Upa*_*Jah 6

知道了(使用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);
});
Run Code Online (Sandbox Code Playgroud)


Kam*_*iya 6

更新:新的Flutter插件permission_handler已经处理了下面的事情,所以将你的插件更改为permission_handler

如果您正在 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,系统会立即授予该权限,而不提示用户。

权限插件仅询问 READ_EXTERNAL_STORAGE 权限Permission.requestPermissions([PermissionName.Storage]);所以你需要请求WRITE_EXTERNAL_STORAGE。

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

permission:
    git:
    url: https://github.com/kamlesh9070/permission
Run Code Online (Sandbox Code Playgroud)