androidcapacitorfile-storage

Capacitor readdir shows no files despite the directory having data


Hi I'm making an application using Ionic Vue and Capacitor to read files from my Android device. I already added the file permissions to read and write.

I have also placed my files in the path Phone/Android/data/io.ionic.starter/files which is also the path the code to write a file in Ionic created a sample file successfully.

And finally, I have used the following functions to try to list all the files inside my application folder:

try {
const fList = await Filesystem.readdir({
  path: '',
  directory: Directory.External,
}).then(res => {
  console.log("CALLBACK READING");
  console.log(res);
  console.log("%%%%%%%%%%%%%%%%%%%%%%");
}).catch(err => {
  console.log("Error reading the file directory");
  console.log(err);
});

console.log("FILES TESTED OUTSIDE");
console.log(fList);
console.log("###################");
} catch(err){
  console.log("ERROR READING FILE");
  console.log(err);
}

Nonetheless the CALLBACK READING function returns nothing while the FILES TESTED OUTSIDE block returns "undefined". I spent an entire day testing different routes and application combinations, my files were created manually and placed inside my application folder. I already tested writing a file and it works, but just doesn't seem to read the files in the directory.

What am I missing? And is there any way I can more easily debug the error?


Solution

  • Modify your file_paths.xml file and add the access to root directory like this:

    <?xml version="1.0" encoding="utf-8"?>
    <paths>
    <root-path name="root" path="." />
    </paths>
    

    this file is located in android/app/src/main/res/xml you also need to add permission to External in Android.Manifest(effective for android <=10) android:requestLegacyExternalStorage="true">