[TIMOB-28231] Android: Change Ti.Filesystem.externalStorageDirectory to use scoped storage
GitHub Issue | n/a |
---|---|
Type | Improvement |
Priority | High |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2020-11-16T21:39:39.000+0000 |
Affected Version/s | n/a |
Fix Version/s | Release 9.3.0 |
Components | Android |
Labels | android, externalstorage, filesystem |
Reporter | Joshua Quick |
Assignee | Joshua Quick |
Created | 2020-11-06T04:33:16.000+0000 |
Updated | 2020-11-16T21:39:39.000+0000 |
Description
*Summary:*
The Android [externalStorageDirectory](https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Filesystem-property-externalStorageDirectory) is currently set to a location outside of "scoped storage". As of Android 10, you cannot write to this location unless you set "AndroidManfiest.xml"
<application/>
attribute requestLegacyExternalStorage
to true
.
https://developer.android.com/about/versions/11/privacy/storage
*Solution:*
Use the folder location returned by the Java [Context.getExternalFilesDir()](https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)) method. This is a private sandboxed directory for the app that falls under scoped storage.
*Note 1:*
With this change, you do *+NOT+* need the following permissions anymore.
* READ_EXTERNAL_STORAGE
* WRITE_EXTERNAL_STORAGE
*Note 2:*
With this change, you do *+NOT+* need to use the following APIs anymore.
* [Ti.Filesystem.hasStoragePermissions()](https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Filesystem-method-hasStoragePermissions)
* [Ti.Filesystem.requestStoragePermissions()](https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Filesystem-method-requestStoragePermissions)
*Note 3:*
The new folder location supports automatic back-up by the Android OS. This means when you uninstall and re-install the app, the files you've written to external storage will be restored.
*Work-Around:*
For apps built with Titanium 8.3.0 - 9.2x, you can read/write to externalStorageDirectory
by doing the following in your "tiapp.xml" file.
<ti:app>
<android xmlns:android="http://schemas.android.com/apk/res/android">
<manifest>
<!-- You MUST target Android 10 for "requestLegacyExternalStorage" support. -->
<!-- The "requestLegacyExternalStorage" attribute is ignored by Android 11. -->
<uses-sdk android:targetSdkVersion="29"/>
<application android:requestLegacyExternalStorage="true"/>
</manifest>
</android>
</ti:app>
Attachments
File | Date | Size |
---|---|---|
FileExternalTest.js | 2020-11-09T22:04:46.000+0000 | 1447 |
PR (master): https://github.com/appcelerator/titanium_mobile/pull/12253
FR Passed. PR Merged.