Storage updates in Android 11
Feature Request
Support the android 11 new file system based policy.
Motivation Behind Feature
Support the changes that are planned for android 11 (API level 30)
Feature Description
I'm not sure how to fully describe this, but basically support what's documented here: https://developer.android.com/about/versions/11/privacy/storage
Alternatives or Workarounds
Not targeting API level 30 until this is supported?
Seems like the fix that was merge #417 in order to solve #408 will no longer work for android 11 (API level 30). I'm not entirely sure I understand what needs to be done in android in order to get access to the file system, something like opening a file picker in order to get write permissions to a folder? I'd love to help progress this, maybe send a PR if someone can guide me to the relevant place in the code and define the appropriate solution...
Seems like the fix that was merge #417 in order to solve #408 will no longer work for android 11 (API level 30).
Yes, you're correct. In API 29, there is a new file system policy. #417 utilised an available flag to request the old file system system, but API 30 will ignore that flag, forcing app developers to implement the new policy model.
To learn about the new android model, you should probably read up on Scoped Storage. I briefly read up on this last weekend and it appears that some apps may require to migrate their data locations depending on where they store data. Scoped storage means just that, you're access to the filesystem is limited to a certain scope. Parts of the file system that you might have had access before may be impossible to access in API 30. This means app developers should take this time to determine if they need to migrate data. Read the android notice for more information on migration. Given the usual pattern, users have probably until August 2021 before Google force API 30 for new apps, and November 2021 before users are forced to use API 30 on existing apps.
The new MANAGE_EXTERNAL_STORAGE permission I believe will essentially make the app behave pre API 29 without the legacy storage flag, but will likely be highly scrutinised by Google if the app uses it without a justified reason. This could be an opt in feature by documenting how one can add this permission using config-file or edit-config. The users must be targeting API 30 in order to use this permission, it isn't available in API 29.
Lastly, getting familiar with the MediaStore APIs sounds like is quite important, because apps can access some external filesystem storages, for accessing pictures, videos, and other media-related resources.
What needs to be changed exactly in the Apache codebase I'm not so certain myself, I'm not that familiar with the code base.
Adding ' android:requestLegacyExternalStorage="true"' to AndroidManifest.xml solves the problem.
Read more at https://developer.android.com/training/data-storage/use-cases.
@olsbg this won't work for android 11, it does and will work for android 10. I've migrated my entire code that saves file to the external file system to share files that are created in memory to avoid any future limitations...
@olsbg this won't work for android 11, it does and will work for android 10. I've migrated my entire code that saves file to the external file system to share files that are created in memory to avoid any future limitations...
Can you share how you achieved this?
Instead of saving files to the file system I use the social sharing plugin. This way a user is prompted with options on what he wants to do with the file I just created in memory: https://github.com/IsraelHikingMap/Site/blob/4e479a5a92051ae5ec84136ca8c20547a4d11f26/IsraelHiking.Web/sources/application/services/file.service.ts#L152
Thanks, @HarelM , Unfortunately this won't work in my case because I'm trying to read a file
@Vatsov Are you reading a file a user selects? If so I don't think there's an issue that is related to Android 11, or at least I hope not as you open the file picker and this should be OK even in Android 11 as far as I know. The other way I solved the file open is by registering my app to open certain file types and allowing the user to share these files with the app. https://github.com/IsraelHikingMap/Site/blob/4e479a5a92051ae5ec84136ca8c20547a4d11f26/IsraelHiking.Web/config.xml#L45 And: https://github.com/IsraelHikingMap/Site/blob/4e479a5a92051ae5ec84136ca8c20547a4d11f26/IsraelHiking.Web/sources/application/services/open-with.service.ts#L85 Hope that helps... :-)
@HarelM Yes, I’m trying to read the file which the user selects with this.fileChooser.open()
The interesting thing is that this.file.checkFile returns true, but when I try with this.file.readAsText it returns null
So I decided firstly to copy the file to this.file.dataDirectory and after that to read it but the this.file.copyFile but returns FileError {code: 1, message: "NOT_FOUND_ERR"}
FYI
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory + "/Download" used to work but now fails with Code 1 on Android 10 both without specifying android:requestLegacyExternalStorage="true" and with. I can't test on Android 11.
It would be really useful to know if there's a plan for the maintainers to create a new release which makes the plugin work with Android 10 and 11 without the need to manually tweak files like the manifest. Hope there is. This is a really useful plugin.
Thanks
What should work
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory + "/Download" used to work
- This should work as is when targeting API 28 (now forbidden by Google Play)
- This should work when targeting API 29 with the dev version of the plugin (which has the
android:requestLegacyExternalStorage="true"), alternatively you can use the edit-config to add this flag. - This won't work when targeting API 30, as API 30 ignores the
android:requestLegacyExternalStorageattribute.
It's important to read the Android Notes before you target API 30. You may need to migrate your files to another folder to maintain access to them when targeting API 30 using the new APIs.
Who this will affect
This will affect any app that reads or writes to the external file system (aka the cordova.file.external*). If you don't use external file paths, then you probably don't need any changes in your app.
I'll be adding a help wanted label. We don't have a planned solution yet. If a volunteer would take the lead and finding a solution to prepare a PR, I encourage to use our Dev Mailing List so that we can discuss requirements and potential solutions.
Useful Links: https://developer.android.com/about/versions/11/privacy/storage
@breautek Thanks for the quick response and useful summary. I'll have to think about this :-)
I've recently implanted a small android app which had to save images to a folder/media storage so I might publish my conclusions here once I'm done...
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory + "/Download" used to work
- This should work when targeting API 29 with the dev version of the plugin (which has the
android:requestLegacyExternalStorage="true"),
Did you mean running cordova plugin add https://github.com/apache/cordova-plugin-file.git
Will be this backward compatible for API <= 28 also?
Will be this backward compatible for API <= 28 also?
Yes, but you still need to use build tools version 29, otherwise the build will fail to recognize android:requestLegacyExternalStorage.
Great, thanks, indeed I made
cordova plugin rm cordova-plugin-file
cordova plugin add https://github.com/apache/cordova-plugin-file.git
I tested it now and it allows me to use cordova.file.externalRootDirectory + "/Download" in Android 10 (API 29)
Yes, but you still need to use build tools version 29, otherwise the build will fail to recognize
android:requestLegacyExternalStorage.
Did you mean this in config.xml?
<preference name="android-targetSdkVersion" value="29"/>
Thanks in advance
I tested it now and it allows me to use cordova.file.externalRootDirectory + "/Download" in Android 10 (API 29)
Yes, but you still need to use build tools version 29, otherwise the build will fail to recognize android:requestLegacyExternalStorage.
Did you mean this in config.xml?
"Build Tools" is part of the Android SDK, you can download it using the Android Studio's SDK Manager tool.

@breautek thanks. I also used your answer, citing the source, to reply on stackoverflow the open issue.
Adding android:requestLegacyExternalStorage="true" is throwing AAPT: error: attribute android:requestLegacyExternalStorage not found. Because cordova 9.x.x is building with SDK-28 even i mention targetSdkVersion as 29. Is there any workaround for that?
You need to be using build tools 29.
https://cordova.apache.org/docs/en/dev/guide/platforms/android/index.html#adding-sdk-packages