AI2 Files block - shared scope file storage cannot read / write text files to media folders

Hi Johannes,

Thanks for your comment and I appreciate you trying to save me time by sending the APK but I'm afraid I don't download APKs unless they are from Google Play. I implemented the app by hand from your blocks diagram to ensure I hadn't missed anything and with the file options in Designer set to Shared scope and with the read and write permissions ticked. The app didn't work on Android 11. On pressing the 'Save' button it produced an error 908 "The Permission Write_ExternalStorage has been denied". I believe this is because the leading forward stroke on your file path invokes the Android 10 Legacy option.

Reference: Storage (used in my first post)

"Legacy: Files will be read from and written to the file system using the App Inventor rules prior to release nb187. That is, file names starting with a single / will be read from and written to the root of the external storage directory, e.g., /sdcard/. Legacy functionality will not work on Android 11 or later."

Reference: Storage updates in Android 11  |  Android Developers (used in Nishyanth's post, marked intended behaviour).

"Apps that run on Android 11 but target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows apps to temporarily opt out of the changes associated with scoped storage, such as granting access to different directories and different types of media files. After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag."

The original question remains open. The AI2 File component is unable to read or write shared text files in the Download and Document media folders with the current version of Android 11. Is this a bug or is it intended?".

How did you set the default file scope in the screen properties?

See again

Taifun

1 Like

Hi Taifun,

I set the scope to Shared in the screen and ticked the read and write permissions, as my question is whether the AI2 Files component when set to Shared Storage scope is working as intended or has a bug. When set to Legacy there is no problem other than the fact that Android 11 no longer recognises the Legacy option.

In the past, I've used your and others' file extensions and they have worked very effectively. Now I'm trying to prepare the app for distribution and want to ensure it will work on Android 11 and can be distributed by Google Play.

Thanks for the question. Peter

It looks like there is a misunderstanding concerning scopes... there are 2 different ones..

In the properties of the Screen you have to set the default file scope to Legacy

And in the properties of the file component you have to set the file scope to Shared

Taifun

2 Likes

Hi Taifun,

I think that is only true if you intend to sometimes use versions of Android released before Android 11 and I see no requirement to do this in the MIT Storage definition:

http://ai2.appinventor.mit.edu/reference/components/storage.html#File

I can see it would be useful to set the default to Legacy in an App that is intended for many versions of Android. But it would compromise the validity of any test of Files for use in Android 11. I have taken great pains to avoid this kind of compromise in my tests. I should also mention that for these tests I always set the File component scope to Shared as well as the screen scope property.

Hi Taifun,

I have tested my test app and Johannes test app with the screen storage scope set to Legacy and the file component property to Shared. There is no change in the response for either. They both show "Error 908: The permission Write_External_Storage has been denied. Please enable it in the Settings app". These permissions don't exist in the Android 11 Settings App and the alternative Files/Media permission has already been set for these apps in the phone I am using.

In the Designer:

This is the only way to have both storage permissions (READ and WRITE) declared in the manifest.

1 Like

Both storage permissions are required on all Android versions. Unfortunately incorrectly even on Android 11+ (meaning it should work on Android 11+ without any storage permissions).

In order for both permissions to be declared in the Manifest on all Android version (even on Android 11+), → DefaultFileScope must be set to → Legacy in the designer.

Note: WRITE permission is no longer avialable on Android 11, but is still wrongly needed to make it work on Android 11+ devices.

Check this app (especially on your Android 11+ devices). Download the aia, build the APK and install it:
textFile_save.aia (2.7 KB)

Screenshot - Android 11 & 12

grafik

Blocks

I've already pointed out this bug countless times, last also on Niotron.

This is definitely my last statement that I will make on this subject. Too many times I've asked the MIT team for an answer/clarification on this.

4 Likes

I have downloaded and run your aia but I get the same errors. See attached images



Did you test with Companion or the APK?

Add a 2 at the end of the path, like this:

/Documents/myTextFile2.txt

grafik

and try again.

Explanation: If this file "myTextFile.txt" has already been created by another app, it cannot be overwritten (on Android 11+). Therefore the file name has to be changed.

2 Likes

Thank you all. Taifun correctly identified that I was confused by the need for two apparently conflicting assignments of the Default File Scope and the read and write permissions and that the screen Default File Scope would have to be set to Legacy. I also didn't realise that this was the essential modification in Anke's app which resolves part of the question raised in this thread.

Hi Anke, Thank you for the App and the very clear explanation. I had acknowledged your identification of this problem earlier in this thread but didn't understand it correctly until I saw your explanation just now. I also mentioned earlier that the AI2 Shared File Scope allows an app to read, write and export files that it generates, but does not allow an external text media file to be read from the Download or Documents folders and generates the "Error 2102: The File Download/Filename.txt could not be opened". This is not corrected by setting the Screen Default File Scope to Legacy. My question is whether this is intended behaviour or a bug?

To access (read / write) non-media files, that are not created by the app itself, you must use the Storage Access Framework (SAF). See also here from my overview → Some basic on Android storage:

1 Like

Here is a simple example how it works using SAF:

2 Likes

Hi Anke, Taifun, Johannes and Nishyanth.

Many thanks, for listening and answering all my questions - very greatly appreciated.

Peter

3 Likes

That worked. However, I cannot see the file until I reboot my Android phone. Closing the application is not enough. Any ideas?

The file should be created immediately, should be visible and located in this folder:
/storage/emulated/0/Documents/

grafik

Click on the 3 dots (top right) and click Refresh:

grafik

2 Likes

I would like to take the opportunity to ask @ewpatton again about some feedback on this subject...
Taifun

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.