Save file on android

Hello, sorry for a question but I can’t believe this thing… With my app I would like to write a very banal image file on the external memory in Android so that it is then visible in the phone gallery. The documentation reads this

The external storage directory On iOS will use the Documents directory On Android it is the primary external/shared storage directory. It is not accessible on Android 10 unless the app enables legacy external storage by adding android:requestLegacyExternalStorage=“true” in the application tag in AndroidManifest.xml. It is not accessible on Android 11 or later.

I mean… I’m full of apps in my phone that write to external storage folder and files, yet capacitor can’t… But how the heck is that possible? Or am I doing it wrong? Thank you

What I was doing is using the cordova-file-opener plugin for this, but after an OS update you have to delete a line for “REQUEST_INSTALL_PACKAGES” from all the manifest files since that was deprecated.

On iOS, you can use the Cordova package as is, or use the Capacitor Share plugin (doesn’t work on Android to my knowledge because it doesn’t let you save on the device like you can on iOS).

That Capacitor Filesystem Plugin mainly works for the internal app files that nobody can access, normally.

Thanks for your reply.
But… Isnt cordova file opener only for open files?

I need to write file on external storage.
Is it possible with this plugin?

Thanks

Oh right I am a dummy, I totally forgot what I did.

In my case this was my issue. My app created .xlsx and .csv files, and I was using apps that open those files to save to wherever the user wanted. Which is why I used the cordova-file-opener plugin So no, I didn’t fix the issue, or at least how you want it to work. In another project, I used the Share plugin to save locally, but the problem with that process is that on Android it doesn’t have the save option like it does on iOS.

Check the pictures below for the “Save to Files” option. It doesn’t appear on Android. I did find a plugin called Capacitor-File-Opener or something like that, that let me save to Google Drive but that’s about it. I was going to test it on my device to see if I could open files on Google Sheets or MS Excel but right now I am still trying to see if I find a way to do a File Dialog like I can do on Windows.

On iOS:
Screenshot 2023-01-22 at 4.14.09 AM

On Android:
Screenshot 2023-01-22 at 4.16.11 AM

Heyya @chidav770 @bandito Any good progress with this?

I am using Capacitor 4 and trying to save photos from my app to the public storage (Directory.Documents). Only works up to Android 12. Receives permission denied error in Android 13.

I found I could successfully create a folder inside the Documents directory… maybe call it the name of you app. I.E Documents/myApp. I could then write my files to that folder. I did this recently, for an app update on android 12.and I’m sure this was tested and working for android 13. I hope it helps you
here’s a snippet for the folder creation

export function createDirectories() {
    const directories = [
        "/myApp",
        "/myApp/images",
        "/myApp/documents",
    ];

for (const dir of directories) {
  Filesystem.mkdir({
    path: dir,
    directory: Directory.Documents,
    recursive: true,
  });
}

}

Sorry for the late reply.

Permissions changed around Android 12. Right now, you cannot use the default plugin to write on the personal device. There are workarounds, but those will get your app denied until you use the default functionality of the FileSystem. If you don’t plan to upload it to the Play Store, you will have to go into Android Studio and hack it to save where you want (I don’t remember what I did, as it was a long while ago).

Now, there is another class you can use, but I haven’t found a plugin for it, you will have to write it yourself.

As a workaround that doesn’t blacklist my app, I just used the Share plugin.

If you need to use the internal storage you can start here, Android storage use cases and best practices  |  Android Developers, if you decide to write the plugin. On iOS I think Share is the default way, since I can only save on the device that way, even on the Apple apps.

What you did was saved on the App folder, not the system folder for Documents. You can verify on the File Manager. I think the only way to save to the Documents folder is using the Storage.xxx object (.xxx can stand for anything because I don’t remember the exact naming of the class).

1 Like