I am using Angular 9 together with ionic5 and I am trying to save a file on my Android 9 device. Unfortunately, every time I try to write file by using Capacitor, the file is saved to the IndexedDB and not on disc/sdcard. Is there some way to tell ionic that I want to save to sdcard and/or fix my code?
const fileName = decodeURIComponent(attachment.Name);
const savedFile = await Filesystem.writeFile({
path: fileName, // filename.png
data: attachment.Content, // base64 encoded png file
directory: FilesystemDirectory.ExternalStorage,
});
console.log(savedFile.uri);
The app should have permissions to write to the SD card, as this is defined inside AndroidManifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
According to the documentation, this should save the file to the root of the sdcard.
When executing this, the returned URI is /EXTERNAL_STORAGE/filename.png
, but the file is not present on the sdcard, because it was saved inside IndexedDB, as seen on the screenshot from Chrome Inspector attached to the application running on my Android:
After I wrote this question, stackoverflow was able to find a similar one, that was not resolved, but anwsered my question.
The solution is to make sure, that you use Plugins
and not import
:
const { Filesystem } = Plugins;
import { Filesystem } from '@capacitor/core';