Flutter Error - FileSystemException: Creation failed, path = 'storage/emulated/0/myDir' (OS Error: Operation not permitted, errno = 1)

Viewed 6357

I am try to create a folder in my external android device's external storage (path: storage/emulated/0/myDir). But I keep getting the same error. I have searched everywhere on the internet for a solution and have not found one.

I have the following permissions in my Android Manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>

And I have the following code the application tag of my Android Manifest

<application
android:requestLegacyExternalStorage="true"
</application>

Any suggestions of what is going wrong?

4 Answers

I solved this problem with the following solutions: For android 10/Q, I have added this permission in the androidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>

and add this line in application tag:

android:requestLegacyExternalStorage="true"

like this:

<application
    ...
    android:requestLegacyExternalStorage="true">

For android 11/R, add this permission:

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />

In app-user permission handler I'm checking permissions with the following function:

Future<bool> _hasAcceptedPermissions() async {
if (Platform.isAndroid) {
  if (await _requestPermission(Permission.storage) &&
      // access media location needed for android 10/Q
      await _requestPermission(Permission.accessMediaLocation) &&
      // manage external storage needed for android 11/R
      await _requestPermission(Permission.manageExternalStorage)) {
    return true;
  } else {
    return false;
  }
}
if (Platform.isIOS) {
  if (await _requestPermission(Permission.photos)) {
    return true;
  } else {
    return false;
  }
} else {
  // not android or ios
  return false;
}}

Due to recent changes in android OS we have to evolve our appp, I found the solution--- you have to enter the following in your project

  1. in manifest- uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"

  2. in your app- user permission handler

    bool permission = await requestPermissionHelper(Permission.manageExternalStorage);

in All Manifest Files

1. <manifest xmlns:android="http://schemas.android.com/apk/res/android" **xmlns:tools="schemas.android.com/tools**"

2. <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage"/>

Future getStoragePermission() async {
  PermissionStatus status = await Permission.storage.request();
  //PermissionStatus status1 = await Permission.accessMediaLocation.request();
  PermissionStatus status2 = await Permission.manageExternalStorage.request();
  print('status $status   -> $status2');
  if (status.isGranted && status2.isGranted) {
    return true;
  } else if (status.isPermanentlyDenied || status2.isPermanentlyDenied) {
    await openAppSettings();
  } else if (status.isDenied) {
    print('Permission Denied');
  }
}

Figured out the problem, android has updated android 11 to include scoped storage so requires an extra permission from the app user

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
        tools:ignore="ScopedStorage" />
Related