39

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);
}
0

11 Answers 11

70

In Android Q, you need to add the Following Lines in AndroidManifest file:

 <application
      android:requestLegacyExternalStorage="true"
3
  • 2
    is it related to the change of targetSdkVersion 29? Dec 26, 2020 at 16:05
  • 2
    Yes, make sense. If your targetSdkVersion is 28, don't need this line. If tour targetSdkVersion is 29 or more, need this line May 15, 2021 at 10:31
  • 2
    After adding this, if you get error: attribute android:requestLegacyExternalStorage not found, check this answer. Specifically, make sure you have compileSdkVersion = 29 in android/app/build.gradle May 20, 2021 at 19:24
28

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"
    >
2
  • 2
    This work for me... Thank. I put android:requestLegacyExternalStorage="true" it work fine now Apr 7, 2021 at 19:48
  • Yes this applies only after android 10Q Apr 8, 2021 at 7:54
24

I had the same problem

My solve:

use permission_handler

import 'package:permission_handler/permission_handler.dart';

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

0
16

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

7
  • 2
    thank you for your reply, I already put these permission but still have error
    – qing
    Mar 6, 2020 at 6:26
  • @qing are you running this code in real device or emulator? Mar 6, 2020 at 6:27
  • run in real device
    – qing
    Mar 6, 2020 at 6:28
  • 1
    So go to the setting/apps and check your application's permissions too in your real device Mar 6, 2020 at 6:30
  • did you check the attached link in my answer? Mar 6, 2020 at 6:35
5

In addition to Rajil TL's answer also include

targetSdkVersion 28

and stop process and run the app. Hot reload is not enough to reflect build.gradle changes.

3

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.

0
2

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>
1

Just change the targetSdkVersion to 28 from the build.gradle file. Rebuild app and run, it's working fine.

1

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){
   try{
      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"
1
  • Adding the "MANAGE_EXTERNAL_STORAGE" along with other permissions worked for me.
    – Khalil
    Sep 7 at 17:15
0

I faced this issue with a Galaxy S9. I couldn't use the last used pictures - I had to click on the gallery and photos tab in the top, the app asked for permission again, and then it worked.

0

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;
  }
});
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.