4

I am trying to write in a .txt file in my flutter program. I am using Permission_handler package and when I run the code (using mi 10) the app ask for permission to access to media and file and I hit allow, and the Debug console says "I/flutter (15890): PermissionStatus.granted" (I mean i did all the part about updating the AndroidManifest.xml or modification on "gradle.properties" , etc.). But when it comes to writing in the file, it throws this error:

E/flutter (15890): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Download/X.txt' (OS Error: Operation not permitted, errno = 1)

I really appreciate any thought on this.

afsane
  • 117
  • 3
  • 11

2 Answers2

0

Mi 10 runs on Android 10. You need to add the following attribute to the <application> element in the AndroidManifest.xml:

android:requestLegacyExternalStorage="true"

This opts you into the legacy storage model.

Also set targetSdkVersion to 29, and compileSdkVersion to 29 or 30 in build.gradle

Joy
  • 191
  • 5
  • 1
    Thanks @Joy for your answer. But it still shows the same error. E/flutter (31011): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Download/X.txt' (OS Error: Operation not permitted, errno = 1) – afsane Apr 24 '21 at 12:18
  • my Mi 10 android version is not 10, it is 11. Do you have any suggestion on it? – afsane Apr 24 '21 at 13:55
  • Try to set `targetSdkVersion` to 29 and `compileSdkVersion` to 30 in `build.gradle`. – Joy Apr 25 '21 at 23:02
  • @afsane did you get any solution for it? – Dharmbir Singh Aug 28 '21 at 05:12
0

I think this happening in Android 11 due to their new changes. So you have to add one more permission (manageExternalStorage).

Like this

if (Platform.isAndroid) {

  Map<Permission, PermissionStatus> statuses = await [
  Permission.manageExternalStorage
  ].request();//Permission.manageExternalStorage
}

Even you have to add read & write permission as well.

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • I am not working on that project anymore, so I cannot try this. But anyway thank you for tacking time. – afsane Sep 27 '21 at 03:05