How do you import data from file on API30, API31?

I need to import csv (text) file downloaded from external service to /Download/
How to read file /Download/import.txt on Android 11/12 (API30/31) ?

1 Like

Why the /Downloads directory?

If the file is required for use in your app, save it to your ASD (application specific directory) where it is read/write by the app.

Use the web component to download your csv file.

If you must, then you can copy the file to /Downloads after saving, using the techniques shown in your other topic with the same question

Note: if using csv data, you do not have to use a file at all, download the data and save it to a tinydb as a list. then work with the data from there on. You can always save out to a file on device or online later. (this is easily done using a google sheet that holds the data in csv format)

1 Like

Thanks for prompt answer.
However I need to be able to exchange files. Android has good file manager (MGail attachements, Google drive, One drive, local filesystem etc.) and chrome web browser - all these save files localy to /Download and not to my application's ASD. Seems to me that shared folders is where files are shared, so I'm looking for a way AppInventor apps can read these files. If AppInventor writes files to /Download others can read it. If /Download is special, /Documents would do too (although /Download is more natural for filemanager and chrome).
So, does anybobody know how to read files from /Download ?
Or maybe there is server I could use from A2I (and how?) by ActionStarter to ACTION_OPEN_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE ?

1 Like
2 Likes

Only the app that created files in this folder (or any of the shared folders) can access them on Android 11+. If these files were created by other apps, you must use SAF to access (read) them.

For more details, see here:

1 Like

You could try this (only tested and working in companion on Android 10 & 12)

I have not been able to get the ActivityStarter to open at a specific directory e.g. /Downloads.

Credits @vknow360 for the FileTools extension

Have a "good" read here:

Access documents and other files from shared storage | Android Developers

Thanks. The Activity starts fine, returns uri but FileTools does returns something that can not be assigned to text - error is:
PathFromUri_Error
Did not get further. What I'm doing wrong ?
Conditions: Companion on Android 12

What does ActivityStarter1.ResultUri return ?

Because there is no way to do this, neither in AI2, nor Kodular or other distros (at least not via activity starter or SAF).

Btw, this question has been asked/answered a few times here and on the Kodular forum.

and what type of document is that?

If i return a text file I get this:

content://com.android.externalstorage.documents/document/home%3AnewText1.txt
1 Like

And I get this on Android 11:

content://com.android.externalstorage.documents/document/primary%3ADocuments%2FmyFile_2.txt
1 Like

it was a.txt file (4byte long noot.txt and 1.86kB long import.txt) in both cases real text files.

Ok, I restarted AppInventor and all devices, removed not needed extensions (SAF and MFile) and repeated tests with different file: /Documents/ximport.txt that I downloaded using native filemanager of Android12 from my googledrive. Got more reasonable uri that was successfully decoded into /Documents/ximport.txt (after removing /storage/emulated/0). Now File component set to Shared. reports 2102 ... file could not be opened. Device: Companion on Android 12.
blocks:


The DefaultScope of File1 is Shared WritePermission and ReadPermission checked. Screen1 DefaultScope is Shared. ACtivitySTarter1 fields is designer empty.
result of picking ximport.txt :

What default Filescope do u set (screen1 and File) ? Any other ideas ? Maybe some perrmissions requests ?

Which means, your app is not owner of the downloaded file and therefore you can't read it using the file component, see also the overview by @Anke here

You can

  1. download it using your own app and then use the file component to read it, or
  2. use the SAF extension

Taifun

Thanks a lot @TIMAI2 for support, basic flow and example (know-how is equally important as tools).
Thanks a lot Sunny @vknow360 for great extension and pointing me to it.

I've read file written to my device /Documents shared folder by Android 12 native file manager (from my Google drive) using Sunny @vknow360 SAF extension (GitHub - vknow360/SAF: App Inventor implementation of Storage Access Framework). As one can see do do not fully control nor understand process (will still have to read docs on SAF) so forgive me possibly "strange" parameters to OpenSingleDocument. Now I have to incorporate it in real project ... lets see if it works :wink:

Steps (as explained by @TIMAI2 ):

  1. Choose file and 2. get uri (async GotUri)
  2. Open file using uri and 4. handle text returned (async GotReadResult)

Try this:

Thanks @Anke. You are right ResultUri was the key (I thought callback returns usable result i.e. uri).
I think I'll stick for a while with SAF as it gives me results within single component.

Thanks for your continuing 1st Class support.