Ionic v4 Local File Storage with Capacitor FileSystem API


#1

Hello,
Currently i am working on a Ionic 4 capacitor project. In my project i have many JSON files which i want to manage using Capacitor Native Filesystem API.

Inside Ionic v4 application src directory have another directory called data where i store all of my local storage json files.

In Application Code:

ngOnInit(){
   async fileRead() {
       let contents = await Filesystem.readFile({
       path: 'data/filename.json',
       directory: FilesystemDirectory.Documents,
       encoding: FilesystemEncoding.UTF8
  });
  console.log(contents);
  }
}

In Android LogCat Error:

10-23 15:08:36.909 20558-20677/app.xplatform.blank E/Capacitor/Plugin: File does not exist
java.io.FileNotFoundException: /data/data/app.xplatform.blank/files/data/filename.json: open failed: ENOENT (No such file or directory)
    at libcore.io.IoBridge.open(IoBridge.java:456)
    at java.io.FileInputStream.<init>(FileInputStream.java:76)
    at com.getcapacitor.plugin.Filesystem.getInputStream(Filesystem.java:103)
    at com.getcapacitor.plugin.Filesystem.readFile(Filesystem.java:154)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:99)
    at com.getcapacitor.Bridge$2.run(Bridge.java:473)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:160)
    at android.os.HandlerThread.run(HandlerThread.java:61)
 Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    at libcore.io.Posix.open(Native Method)
    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    at libcore.io.IoBridge.open(IoBridge.java:442)

Questions:

  1. Where should i save my json files, so that capacitor will fix this error?
  2. Is there any good tutorial on Capacitor FileSystem, except official documents?

Thank you.