Fluttter android (OS Error: Permission denied, errno = 13) write to Downloads folder

171 views Asked by At

I'm trying to copy a file that's in the device's temporary folder to the Downloads folder, but I'm getting this error in the file.copy function:

PathAccessException: Cannot copy file to '/storage/emulated/0/Download/test.pdf', path = '/data/user/0/myapp/cache/file-pdf.pdf' (OS Error: Permission denied, errno = 13)

Here is the code:

await Permission.storage.request();
Directory downloadFolder = Directory("/storage/emulated/0/Download/")
String pdfName = "test";
await file.copy("${downloadFolder.path}$pdfName.pdf");

I'm using permission_handler: ^11.1.0, and I wrote this to my AndroidManifest.xml:

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

Here is my build.gradle configuration:

android {
    compileSdk flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion flutter.targetSdkVersion
    }
}

Flutter doctor output:

[✓] Flutter (Channel beta, 3.18.0-0.2.pre, on macOS 14.2.1 23C71 darwin-arm64, locale pt-BR)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2022.3)
[✓] VS Code (version 1.85.1)
[✓] Connected device (2 available)
[✓] Network resources

I need this code to run in Android 10 to 13.

1

There are 1 answers

0
Goat Solutions On

Saving files in the Downloads folder worked (on Android 10 to 13) when I added the tag below to the AndroidManifest.xml file:

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

Thanks to @blackapps for help.