Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Unable to create directory (OS Error: Read-only file system)

Tags:

flutter

I was following - how to create directory from 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);
});

This is the error I am getting:

FileSystemException: Creation failed, path = 'dir' (OS Error: Read-only file system, errno = 30)

I have enabled Storage(WRITE_EXTERNAL_STORAGE) permission. (Android device)

What am I missing?

like image 928
UpaJah Avatar asked Jul 31 '18 06:07

UpaJah


2 Answers

Got it (Using path_provider plugin to get correct path)

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);
});
like image 85
UpaJah Avatar answered Oct 22 '22 19:10

UpaJah


Update : New Flutter plugin permission_handler have taken care below thing so change your plugin to permission_handler

If you are testing in Android Oreo and want to write file in external storage then you need to ask WRITE_EXTERNAL_STORAGE

According to "https://developer.android.com/about/versions/oreo/android-8.0-changes.html#rmp"

If the app targets Android 8.0 (API level 26), the system grants only READ_EXTERNAL_STORAGE at that time; however, if the app later requests WRITE_EXTERNAL_STORAGE, the system immediately grants that privilege without prompting the user.

Permission Plugin ask only READ_EXTERNAL_STORAGE permission by Permission.requestPermissions([PermissionName.Storage]); so You need to ask for WRITE_EXTERNAL_STORAGE.

You can use my repo for asking WRITE_EXTERNAL_STORAGE in pubspec.yaml:

permission:
    git:
    url: https://github.com/kamlesh9070/permission
like image 4
Kamlesh Kanazariya Avatar answered Oct 22 '22 17:10

Kamlesh Kanazariya