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

The question and background
Regarding AI2 apps in mobile phones using Android 11. Can AI2 scoped storage 'File' blocks read external files, and write app generated text files between the app and the shared media folders:'Documents' and 'Download'. These links suggest they can:
http://ai2.appinventor.mit.edu/reference/components/storage.html
https://developer.android.com/training/data-storage/shared/documents-files
https://source.android.com/devices/storage/scoped
Also, the files and media permissions available for AI2 apps in the Android 11 settings appear to confirm this. However, tests using the attached App were unable to read or write text files between the App and the Download or Documents media folders.

T2File1.aia (32.0 KB)

Method
In the test App the read and write permissions were both ticked in the Designer page.
Read and Write permissions could also be manually requested. The scope could be switched between 'App' and 'Shared'. The default was set to 'Shared' in the Designer page, and it was also manually set to shared prior to each test. The file existence in the folder was checked prior to each test. The test file path was either <Document/FileName.txt> or <Download/FileName.txt> with no leading '/'. The files used to test the write function were all generated by the test app. Only one copy was made of each file. The units tested were a Nokia 2.1 mobile phone with Android 10 and a Nokia 5.4 with Android 11.

Results
The Nokia 2.1 had an SD card formatted as Internal Shared and the File storage scope set to shared. On first using the App the read and write permissions buttons had to be used despite the read and write boxes being already ticked in the Designer (reference Anke's posts). However, the test App could write and read the test files to and from the Internal Shared Storage 'Document' and 'Download' shared directories.

The Nokia 5.4 mobile phone had Android 11 and no SD card, the storage file scope was set to shared and the App 'Files and Media permission' in the phone settings was switched on. If the App tried to read a text file from the Internal Storage 'Document' or 'Download' shared directories, Error 2102 was flagged saying: 'The File cannot be read'. If the App tried to write a text file to the Internal Storage Document or Download shared directories, Error 908 was flagged saying 'The Write external storage is denied'. Manually requesting a read external storage permission had no effect. Manually requesting a write external storage permission produced another 908 error message.

Interpretation ?

Have I missed anything? Do I need to install an SD card formatted for external storage to share files when using Android 11?

Is the Apps inability with Android 11 to read or write shared text files in the Download and Document media folders a bug or is it intended? If it is intended what is the proper usage for the shared storage scope?

Should I report this as a Bug?

This is intended behavior.

You could try doing what's suggested in the post below:

1 Like

Try this on your Nokia 5.4 mobile phone with Android 11:
EDIT: DefaultFileScope = Legacy
T2File1_2.aia (32.2 KB)

1 Like

Hi Nishyanth Kumar,

Thank you for your comments but unfortunately, they are not applicable to my question so I should make the following clarifications:

i) Under the test conditions described, text files created by the App with the latest version of Android 11 cannot be read with the shared storage scope File component and/or saved as new text (in the shared folder 'Documents' or in the shared folder 'Download'). However, they can be so read or saved if used with Android 10.
2) The Apps I am using are intended for distribution to citizen scientists, So the modifications you describe for text files that are created external to the App are not going to be viable because:

  • The Apps need to be future-proofed i.e. be capable of working in current and future implementations of Android as well as past SDK versions and should not rely on legacy versions of AI2 or Android. (I already have many working legacy versions).
  • The apps are intended to be distributed publicly via Google Play, or assembled from an aia file using AI2, by newcomers to AI2, without the need to modify the Manifest outside the Design Page facilities available within AI2

3). As a matter of general concern, my question also asks whether the Apps File component inability to read or write shared text files in the Download and Document media folders, with the current version of Android 11, is a bug or is it intended? If it is intended what is the proper usage for the AI2 shared storage scope?

1 Like

Hi Johannes,

Thank you also but my response to Nishyanth precludes using legacy versions and I am trying to clarify whether the Files block is not intended to save or read text file media to the Download or whether there is a bug that needs to be fixed.

Nishwanth makes the interesting point that it should be possible to read and save files created by the App itself, and that was my understanding also. However this can't be done with the latest version of Android 11 unless I am missing something fundamental - hence my question on whether I should report this as a bug.

Did you try my test app (T2File1_2.aia)?

1 Like

Hi Johannes,

Yes your App appears to be identical to earlier test versions I have tried. I assume you have set the Storage scope to shared. Have you tested it successfully using an APK with Android 11? If so then I would be very interested to make a close comparison because my version doesn't work with Android 11..

P.S. I will download your version and see if it works on my phone.

Change the Textbox text from "abcde" to "abcde2" before building the apk (and do not try with Companion).

Absolutely (I never use Companion for this kind of test and always test with new text files). It's 11pm in Australia and I have to look after a sick relative so I'll close down now and get back to you tomorrow. Thanks for this opportunity.

Hi Johannes and Nishyanth,

I checked and saw that the change Johannes is suggesting is to use the Legacy mode. This will not work on Android 11 see Nishyanth's link labelled "Intended behaviour". I did run the Test App using Legacy storage scope to prove the point and indeed the Legacy storage scope doesn't work on Android 11. My goal is to keep my Apps up to date for public use and using Legacy mode no longer achieves this.

The point that Nishyanth makes (referencing a post by Anke) is that "it is no problem to read it with the File component and / or to save new text (in the shared folder /Documents)" This is exactly what I'm trying and failing to do in Android 11. And this is the intended behaviour I'm testing. It may be that what was true last year when Anke made the post is no longer true. But is that a bug or intended behaviour!

Try this app:

Android 11 and 12:
grafik

1 Like

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