How to solve OS Error: Permission denied, errno = 13 in flutter

Viewed 38842

I am new in Flutter. I want to store my document file into '/storage/emulated/0/Download/'. I have error about 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);
}
9 Answers

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

 <application
      android:requestLegacyExternalStorage="true"

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

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

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

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.

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

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>

I faced this issue at a Galaxy s9. I couldnt use the last used pictures - I had to click on the galary and fotos tab in the top, than the app asked for permission again and than it worked. Maybe somebody will need this info

Related