Issue
I 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. A Pixel 3 running Android 10 does NOT work. It also 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.
Solution
Android implemented a new storage policy in Android 10 (API level 29) that is being enforced in Android 11 (API level 30) called Scoped Storage. By default it prevents an app from accessing the general file system, in your case the Downloads folder.
I suggest a long term solution of re-architecting your system to not require information from the Downloads folder since there might be sensitive information there that most apps should have no business accessing. If you do need access, however, there is a new permission you have to use: MANAGE_EXTERNAL_STORAGE.
For Android 10, a flag was added to cordova-plugin-file recently to ensure the OS uses the old file policy, so your code should work if you're using the latest plugin version. If you need to install it directly from the repo:
cordova plugin rm cordova-plugin-file --force
cordova plugin add https://github.com/apache/cordova-plugin-file
Subscribe to the related issue for more.
Answered By - Alex Steinberg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.