Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I want to store my document file into
'/storage/emulated/0/Download/'
. I got this error :
Unhandled Exception: FileSystemException: Cannot open file, path = '/storage/emulated/0/Download/file.pdf' (OS Error: Permission denied, errno = 13)
Here is my code:
void download() async {
http.Response response = await http.post(url, headers: {"Accept": "application/json", HttpHeaders.authorizationHeader: 'Bearer'}});
File file = new File('/storage/emulated/0/Download/file.pdf');
await file.writeAsBytes(response.bodyBytes);
–
–
Just add the following lines in to your android manifest if you're on android 10/Q
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"
–
before download action you need check storage permission:
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
hope to help some body
Make sure you defined permissions in AndroidManifest file
like this :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xxx.yyy">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>
for more info try out this link
–
In my cases none of the solutions worked, finally what worked was to remove from android/app/build.gradle the reference to "targetSdkVersion XX" inside defaultConfig{}
Now everything works as expected, I guess it is a bug in the framework.
You have to add android:requestLegacyExternalStorage="true"
this in AndroidManifest.xml file.
<application
android:label="app_name"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true"
</application>
My case was that it was possible to access directory. but couldn't read file.
final path= "${await ExternalPath.getExternalStoragePublicDirectory(ExternalPath.DIRECTORY_DOCUMENTS)}/sub";
final dir= Directory(path);
final l = dir.listSync(); // 1. works fine until here
for( final d in l){
final str = await File("${d.path}/info.json").readAsString(); // 2. permission error here
}catch(e){
print(e);
for solving this problem, I included android.permission.MANAGE_EXTERNAL_STORAGE permission.
like belows.
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"
After long Research i find this Solution and it will work on all Android 10+
Just change await Permission.storage.request() to await Permission.manageExternalStorage.request() in your Permission Handler or Requester class.
Because this change will give you full access to Media Storage excluding Restricted Storage.
Detail Example:
First Create a method.
Future<AppPermissionStatus> askForPhotoGalleryPermission(
BuildContext context) async {
var status = Platform.isIOS
? await Permission.photos.request()
: await Permission.manageExternalStorage.request();
AppPermissionStatus permissionStatus = AppPermissionStatus.NotAvailable;
if (status.isGranted) {
permissionStatus = AppPermissionStatus.Granted;
return permissionStatus;
} else if (status.isDenied) {
permissionStatus = AppPermissionStatus.Denied;
return permissionStatus;
} else if (status.isRestricted) {
permissionStatus = AppPermissionStatus.Restricted;
return permissionStatus;
} else if (status.isPermanentlyDenied) {
permissionStatus = AppPermissionStatus.PermanentlyDenied;
return permissionStatus;
How to call this Method.
void getRequiredPermission(BuildContext context) async {
AppPermissionStatus permissionStatus =
await AppPermission().askForPhotoGalleryPermission(context);
setState(() {
if (permissionStatus == AppPermissionStatus.Granted) {
hasPermission = true;
} else if (permissionStatus == AppPermissionStatus.PermanentlyDenied) {
hasPermission = false;
} else {
hasPermission = false;
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.