简体   繁体   English

使用 @ionic-native/file/ngx 从 Android 下载文件夹读取文本文件时出现问题 - Android 11

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

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.我有一个 Ionic 5 应用程序,我正在使用 @ionic-native/file/ngx 和 cordova-plugin-file 插件将文本文件读入我在 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.这适用于运行 Android 9 的几部手机。运行 Android 10 的 Pixel 3 不起作用。 It also does NOT work on a Pixel running Android 11. The promise returns null.它也不适用于运行 Android 11 的 Pixel。promise 返回 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使用 checkDir() 验证目录是否存在 使用 FileChooser 和 filePath.resolveNativePath 使用 androidPermissions READ_EXTERNAL_STORAGE 设置为 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.任何关于我能做些什么来解决这个问题的想法都将不胜感激。

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 . Android 在 Android 10(API 级别 29)中实施了一项新的存储策略,该策略在 Android 11(API 级别 30)中实施,称为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 .但是,如果您确实需要访问权限,则必须使用一个新权限: 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.对于 Android 10,最近向 cordova-plugin-file 添加了一个标志,以确保操作系统使用旧的文件策略,因此如果您使用的是最新的插件版本,您的代码应该可以正常工作 If you need to install it directly from the repo:如果您需要直接从 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.订阅相关问题了解更多。

In my case I had to add <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" /> to my AndroidManifest.xml, then I got into Settings>Privacy>Permission manager>Files and media and searched my app there, then when you click it you'll see the option "Allow management of all files".在我的例子中,我必须将<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />添加到我的 AndroidManifest.xml,然后我进入设置>隐私>权限管理器>文件和媒体并在那里搜索我的应用程序,然后当您单击它时,您将看到“允许管理所有文件”选项。

You have to do it manually, I haven't found yet a way to automatize the process, but I hope you can use this as a solution.你必须手动完成,我还没有找到一种方法来自动化这个过程,但我希望你可以使用它作为解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM