Save CSV data / list as a file

I'm trying to save CSV data/list as a file. Background: I don't want to lose the data anyway (e.g. app deinstallation, reinstallation) and want to have direct access to the file (e.g. transfer to the PC).

I've studied some articles on the forum, especially this one: [Some basics on Android storage system]

Unfortunately I can't get any further. I've tried everything possible. I cannot find the generated file on the tablet. Don't know if one was even created. I ask for a hint or nudge in the right direction. The relevant coding blocks below. All other works find (display the data on screen, sort it, etc.)

Android Version is 13; SD Card ist available; tablet is connected via USB (app preview via USB connection to the real device);

You should find the file HighScore.csv in shared storage /Documents
Do you only want to store the total, not the data?

Taifun

Thank you for your reply :slight_smile: I will check it. Did it several times but will check it again. total includes list head and data. i did not find another solution to sort the content. if i have all in one list before sorting the headline is shown on the bottom of the list... but the way i explored seams to work.

i think the problem is shared storage /documents. i thought that shared as a scope means that it is the way to characterize the "privacy". What I want to reach is store the file on sd card in folder "documents". do i have to choose another scope?

The file component is not able to store a file on an external (removable) sdcard... you only can store files on the internal (emulated) sdcard

Writing to the external (removable) SDcard is no longer possible since Android 4.4 (API 19, KitKat ).

Taifun

2 Likes

Okay. Than I missunderstood the many things I read. So ist that correct: "/Documents/HighScore.csv" or must it be: "Documents/HighScore.csv" (without slash) or only "HighScore.csv"?

Where is the folder where I should find the file? I checked the following:

Interner gemeinsamer Speicher\Android\data\appinventor.ai_<user>.TestScore_X2\files
Interner gemeinsamer Speicher\Android\data\edu.mit.appinventor.aicompanion3\files

And also the subfolder assets if available. Could not find the file HighScore.csv :frowning:

But the main question is: if it is not possible to store a file on sdcard but instead somewere else: will the file still exists if the app is deleted? this in fact is my main objective to persist the data (in that case only a highsore but in other cases sensor values) in a way where i can decide if the file can be deleted or not

This is correct

The first is the ASD of your app while testing using the apk file, the second path is the ASD of the companion app

Find the file in shared storage
Interner gemeinsamer Speicher/Documents

See also Some basics on Android storage system

Of course it is possible, and shared storage is the correct place to store the file

Taifun

2 Likes

If you set FileScope = Shared you can also use "Documents/HighScore.csv". If you set FileScope = Legacy you must use "/Documents/HighScore.csv" (with a leading slash).

1 Like

You are right. Can not explain why it works now. I checked it a lot of times also in the folder you pointed out. Last time i did not check that one because i thought that it is not relevant because of testing with the simulation mode via usb (aistarter) and not with the really installed apk. But in the past there was also no file in that folder. Otherwise i would not have opened that topic. Thanks a lot :slight_smile: You made the weekend glorious for me!

I come back when I fixed some more issues. There was also an error in the handling with the list (head, data, total). Maybe i can simplify it but i have to check. On my first attempts sorting did not work in the simple way i expected it should do.

Falls du es zuerst mit Companion testest, sollte es (auch) funktionieren (also die CSV-Datei im Ordner /Documents erstellt werden). Danach funktioniert es jedoch nicht mit der APK, da die Datei mit einer anderen App (Companion) erstellt wurde und danach nicht mehr von einer anderen App (APK) überschrieben werden kann. Die CSV-Datei muss also zuvor (mit Companion) erst wieder gelöscht werden, damit es mit der APK funktioniert.


If you test it with Companion first, it should (also) work (i.e. the CSV file will be created in the /Documents folder). However, after that it doesn't work with the APK because the file was created with another app (companion) and can't be overwritten by another app (APK) after that. The CSV file must first be deleted (with Companion) so that it works with the APK.

1 Like

I come closer with every minute to the objective. All works. Load, Save. Insert an entry in the highsore is open but i think to know how to do it.

Another thing which not working is the following. I can read the file like this (pick a file, read it and in the event GotText i split it into the parts head and data). Also the tableView shows the right values.

But if I try to open the file automatically in event Screen.initialize nothing happens... Is it to early in the process? How can I achieve that the "default" HighScore file (specified in the variable) will be opend automatically? The variable is also used to save the file... which seams to work correctly.

image

image

Your blocks look correct...

It should work if you do not switch apps, ie. test only in the companion app or using the apk file...
As soon as you switch, remember what @Anke said

Taifun

Thanks for your advice but I have that in mind and thanks for pointing that out.

I deleted all the files were created. Then I saved a new version with my save button. I can open it. All worked. Then I only put the codeblock for filepicker additionally in screen-initialize and it does not work. Still the file can be opend with the file picker.

All described above with the companion only.

Use only the File.ReadFrom block without the filepicker as you did it in your latest screenshot

Taifun

Sorry but I don' t know what you mean. I shall delete the block out of the (how is it called...) "blocks development view"? I dont push the file picker. I only wanted to say that this logic still woks if i use it but opening the file in screen.initialize does not. I don't have the file picker in screen.initialize. This functionality only coexists.

@Taifun
I tried the following: I put the logic from initialize behind a switch (changed event). It also does not work. so it is not a problem of screen.initialize.

image

Then I tried writing a text in a label to make it visible if the logic of GotText is reached. That does only work with the file picker. In other cases when file.ReadFrom is called the event might not be triggered. Or an error occures when opening the file.

image

The last thing i tried for today is the following. I inserted some blocks in screen. inizalize shown below. While I was doing that the app asks for permissions for video, media and so on. I granted it. When testing nothing happens. Also the notifiere does not show anything.

image

It's unbelievable that such seemingly simple things can be so complicated or maybe I'm just stupid.

To read a text file which is owned by your own app normally no permission would be required, see also Some basics on Android storage system
Do you still get that permission dialog if you remove the filepicker component completely?
Let's see what our specialist @Anke is saying..

What you could do is to read the file after these permissions have been granted...

Taifun

Have you considered instead just saving the file off your device to an online resource ? Online storage is free/cheap and easy to use and access from your android device..

Yes, this is a bug on Android 13+ that I've definitely mentioned several times before. READ permission is requested incorrectly. Use my MFile extension to avoid this bug.

@ewpatton

1 Like

Unfortunately, I'm not a fan of the public cloud. If it were possible with a private mini cloud (usb device connected to fritz.box) I would consider it. I can't imagine it being easier.