Issue when reading text file from Android Download folder with @ionic-native/file/ngx - Android 11

have an Ionic 5 app that I am using @ionic-native/file/ngx and the cordova-plugin-file plugin to read in a text file into my app on Android.

My code is as shown below:

const path = this.file.externalRootDirectory + 'Download';

this.file.readAsText(path, 'foo.txt').then((file) => {
   console.log('process file');   
}).catch((e) => {
  console.log('error '+ e); 
});

This works on a couple of phones running Android 9 (I don’t have a device with Android 10). It does NOT work on a Pixel running Android 11. The promise returns null.

Things I have tried:

Used checkDir() to verify that the directory exists Used FileChooser and filePath.resolveNativePath Used androidPermissions READ_EXTERNAL_STORAGE is set to true

Here is my Ionic info

Ionic:

   Ionic CLI                     : 6.12.2 (/Users/.nvm/versions/node/v12.18.3/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.4.3
   @angular-devkit/build-angular : 0.1002.0
   @angular-devkit/schematics    : 10.1.7
   @angular/cli                  : 10.2.0
   @ionic/angular-toolkit        : 2.3.3

Cordova:

   Cordova CLI       : 10.0.0
   Cordova Platforms : android 9.0.0, ios 6.1.1
   Cordova Plugins   : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 5.0.0, (and 15 other plugins)

Utility:

   cordova-res (update available: 0.15.2) : 0.15.1
   native-run (update available: 1.2.2)   : 1.0.0

System:

   ios-sim : 8.0.2
   NodeJS  : v12.18.3 (/Users/.nvm/versions/node/v12.18.3/bin/node)
   npm     : 6.14.8
   OS      : macOS Catalina
   Xcode   : Xcode 12.2 Build version 12B45b

Any thoughts on what I can do to resolve this would be greatly appreciated.

I would use Capacitor’s Filesystem instead of this setup, but assuming you want to stick with Cordova, I would suggest reading through this issue.

Thanks for the response. I am currently 100% Cordova and use a lot of plugins currently not supported by Capacitor.

Is it easy to integrate this one Capacitor plugin and leave the rest of my app using Cordova platform and plugins?

Well, before you go down the Capacitor road, check that issue I linked and make sure your permission setup is copacetic.

Thanks for the comment. I believe my permissions are ok. Like I mentioned in my original post I am using the android-permissions plugin to verify that READ_EXTERNAL_STORAGE is true. The last post in the link you provided states the following which seemed to be my situation. I have permission but the promise is returning null when trying to read.

@fragatak thanks! I’ve the permissions for the storage, but the file content is just “null”. Do you have any idea?

This issue was first reported on October 1st

As of API 29, there is a new file system policy. As long as you are targeting API 29 which the latest version of cordova-android does you can add the following flag to the config.xml to get it working:

requestLegacyExternalStorage