FileSystem write file in Android 13

Hi everyone!
I’m trying to use capacitor/filesystem and write files in download directory, but it doesn’t work (I’m using android 13 API).

Following my code:

async writeRenFile(fileName,data) {
try{
await Filesystem.writeFile({
path: ‘Download’ + fileName,
data: data,
directory: FilesystemDirectory.EXTERNAL_STORAGE,
encoding: Encoding.UTF8,
recursive: false
});
}catch(e){
console.log(e);
}
},

If I remove ‘Download’ from path, it save my pdf file in Android/data/com.myapp/files/fileName, but I can’t access it from my smartphone.
If I kepp ‘Download’ or ‘Downloads’ or any different Directory, it totally doesn’t work.

How can I fix that?
Thank you!

I don’t think you can anymore since WRITE_EXTERNAL_STORAGE was deprecated.

But maybe this can help. I haven’t tried it though. Manage all files on a storage device  |  Android Developers

But basically add MANAGE_EXTERNAL_STORAGE to your manifest. Or maybe add the path/to/your/downloads folder in the files/path.xml in the manifest (if on Android Studio is something like app/res/xml/files_path.xml).

Hello, I changed my code, but I totally don’t know how to configure the “file_paths.xml” in android studio

following my file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>

following my method:

try{
await Filesystem.writeFile({
path: fileName,
data: data,
directory: Directory.Data,
encoding: Encoding.UTF8,
recursive: false
});
}

Can I save my files in android donwloads directory?
Thank you!

Sorry for the late reply. I completely forgot this website existed for a second. :joy:

Ignore my other post and do this instead. I tested on my app and this worked. I haven’t deployed to the Play Store yet, so if you get errors from Google Play Console, just add <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/> if it asks for it.

The READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are deprecated on Android 13 as per the docs.

Instructions*

await Filesystem.writeFile({
path: 'my-directory/' + fileName,
data: data,
directory: Directory.External,
encoding: Encoding.UTF8,
recursive: false
});

Verify the directory prop changes

Now on your phone, go to Android/data/io.my.app/files/my-directory and the directory with the files inside should be there. I couldn’t make it work so that the file writes to the download folder, sorry.

There’s Directory.EXTERNAL_STORAGE of the Directory prop from Capacitor Filesystem class, but the docs said that it is It's not accesible on Android 11 or newer..

Ciao Bandito!
Don’t worry about that! I solved it using download + share file. In this way I can open my file using a “pdf” reader and download my file using it.

Have a nice day,
Lorenzo

1 Like

I did the same thing so the file could be opened directly on Sheets/Excel haha

Could share some resources, how can we implement this