CSV does not save all items and problems saving the file

Hi everybody,
I have a problem when I create a csv file from a list. If I have for example 6 elements in the list “indexes_selected_events”, the csv file only add 3-4 elements, depending the occasion. I do not explain this random behaviour, I do not know if the problem is when the csv values are written (problem to save files in android 11, this is the version of my phone) or if there is an error in the code. Can anyone help me, please?

whast happens, if you share the file a little bit later?
you could use the Clock component to share the file for example 1000ms later

Taifun

Thanks, @Taifun . I will try your proposal and will tell you as soon as possible

It seems that problem is related to the time that the bucle need to save each item. I have tested with different timer interval from 1000 to 20000, and with this latter time, all items are saved.
Now, I will have to adjust the time intervals depending on the length of the list. Any idea?

Thanks, @Taifun . I

Use a global variable to store the header of the file and a loop to add all the rows…

Then use only the File.Save method (without the Append method) to save the complete file

And in the After File Saved event share the file…

Taifun

2 Likes

I solved the last problem thanks to @Taifun , but now, I have a problem when I save the file. Always worked well, using mnt/sdcard/, but after the modifications suggested by @Taifun the problem is that the app does not find the file. Maybe, @bodymindpower can help me. I enclose the block image.

try /storage/emulated/0/and start the path with a slash…
Taifun

2 Likes

… and this should also work:
/sdcard/

See also here.

grafik

1 Like

I have tried with the different options of directories proposed by @bodymindpower but anyone was alright. The error is 2001, “the file could not be found on your device”. I have checked and the file is created in /storage/emulated/0/ but I can not read it to sharing. Any solution?

I observe I wrtie a name first time, I obtain the error, but if I try again with the same name I can sharing the file without problem. The system need to have the file previously.

I have the permission of write and read when I initialize the screen.

thanks

Move this block to the File.AfterFileSaved event.

And this will only work on devices with Android < 10. On Android ≥ 10 the text file is saved in the ASD:

grafik

Thanks, @bodymindpower . I moved the sharing block to file.afterFileSaved event and now sharing does work, in android 11 and android 10. Great!!

Remove the csv file from the root dir of the external storage
/storage/emulated/0/
and try again on one of your Android 10+ devices.
Then it should not work, because the File.SaveFile method writes the text file to the ASD on devices with Android 10+ (and not to the root dir of the external storage).

I have not changed anything, My phone has version 11 of Android. I remove all files csv in /storage/emulated/0/ and I can create the csv file again without problem. I could send you a screen video (in PM) if you have interest in it.

if it works for you it is absoluteliy fine… but just to understand what is happening…
you might want to provide a screenshot of your AfterFileSaved event…

where will the csv be created? in the ASD …
however your blocks up to now try to share the file from the root directory…

Taifun

I attach the screenshot of my AfterFileSaved event and also the save File in order to understand better.

I create the csv file in the storage/emulated/0/ and I read with mnt/sdcard

I would like to clarify that I am an unexperienced user and I don’t really understand the directories with this problem of sdk 29/30 and android 10/11. But now, the app works fine.

Send the aia via PM. I’ll take a look.

Done, thanks.

Ok, you have used the Legacy Mode.
This will no longer work on Android 11 devices when Kodular targets SDK 30, because requestLegacyExternalStorage is ignored then.

ok, In this moment (kodular sdk30), what changes will I have to do? I am lost with this subject. How will it affect to manage save file directory?

in this moment Kodular still targets sdk29, so there is nothing to do…
if you want to be on the safe side for the future, then store the file in the ASD asd share it from there as already said previously…

Taifun

1 Like