Saving data on Android

The place to discuss anything and everything about running your LiveCode on Android

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
DolphinTech
Posts: 10
Joined: Thu Dec 28, 2017 1:33 pm

Saving data on Android

Post by DolphinTech » Sun Jan 14, 2018 12:55 pm

Hello everyone, I have got a new question concerning how to save data (e.g. Highscore) on a android device.
I currently use

Code: Select all

put highscore into url("file:"&specialFolderPath("cache")&"/highscore")
, which works for me.
But is this method "clean" and are there any conventions about this topic? And where exactly is this file saved (i couldn't find it on my phone)?

Another problem is that I cannot save array variables in files. Something like

Code: Select all

put 10 into stats["highscore"]
put stats into url("file:"&specialFolderPath("cache")&"/highscore")
produces an empty file. Are there any ways to fix this or is this a bug.

Thank you

sphere
Posts: 629
Joined: Sat Sep 27, 2014 10:32 am
Location: the Netherlands

Re: Saving data on Android

Post by sphere » Sun Jan 14, 2018 1:14 pm

put your highscore field or whatever in a variable like tHS
then save this variable to a textfile

put tHS into URL ( "file:" & specialFolderPath("Documents") & "/highscore.txt" )

then read out again
put specialFolderPath("Documents") & "/highscore.txt" into tWhatwasit
if there is a file tWhatwasit then
put url("file:" & ttWhatwasit) into toHthis
answer toHthis
Please, stop quoting things my brother did not say. Thank you. Hans Zweistein.

Klaus
Posts: 10169
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Saving data on Android

Post by Klaus » Mon Jan 15, 2018 5:39 pm

Hi DolphinTech,

arrays are binary data, so use the arrayEncode/arrayDecode to save and retrieve array data in text format:
...
put 10 into stats["highscore"]
## Store array in TXT format:
put arrayEncode(stats) into url("file:" & specialFolderPath("documents") & "/highscore")
...
## Read into variable again:
put url("file:" & specialFolderPath("documents") & "/highscore") into tStats
put arrayDecode(stats) tStats
...
Hint:
For persistent data use specialfolderpath("documents")!


Best

Klaus

DolphinTech
Posts: 10
Joined: Thu Dec 28, 2017 1:33 pm

Re: Saving data on Android

Post by DolphinTech » Mon Jan 15, 2018 6:58 pm

Thank you Klaus for your reply.
Obviously, arrayEncode was the function I was looking for.
But is the data stored into specialFolderPath("cache") not persistent?
And can data stored into "documents" be edited easily by others?

Thanks
-DolphinTech

Klaus
Posts: 10169
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Saving data on Android

Post by Klaus » Mon Jan 15, 2018 7:20 pm

Hi DolphinTech,
DolphinTech wrote:
Mon Jan 15, 2018 6:58 pm
But is the data stored into specialFolderPath("cache") not persistent?
it is, but that folder is not being backed up by iTunes, if that is important for you.
DolphinTech wrote:
Mon Jan 15, 2018 6:58 pm
And can data stored into "documents" be edited easily by others?
Everyone who has access to the device may be able to edit the data.
So maybe use encrypt() to encrypt your data before saving, if you have really sensitive data.


Best

Klaus

DolphinTech
Posts: 10
Joined: Thu Dec 28, 2017 1:33 pm

Re: Saving data on Android

Post by DolphinTech » Mon Jan 15, 2018 7:46 pm

I don't have a Mac, so I can't deploy for iOS anyways and that makes the iTunes irrelevant for me.
if you have really sensitive data.
My data has the relevance of a single-player-offline-mobile-game-progress, but i don't want it to be editable with just a text editor.

I could not find the data stored by specialFolderPath("cache") on my phone. Do you know where it is saved?

Thanks

-DolphinTech

Klaus
Posts: 10169
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Saving data on Android

Post by Klaus » Mon Jan 15, 2018 7:56 pm

DolphinTech wrote:
Mon Jan 15, 2018 7:46 pm
I could not find the data stored by specialFolderPath("cache") on my phone. Do you know where it is saved?
Sorry, no idea! Just add a button and check it by yourself on the device:
...
answer specialfolderpath("cache")
...
:D

DolphinTech
Posts: 10
Joined: Thu Dec 28, 2017 1:33 pm

Re: Saving data on Android

Post by DolphinTech » Mon Jan 15, 2018 8:06 pm

Did this.
the path is /data/user/0/com.DolphinTech.minesort/cache
I could not find this path on my phone. any reasons why?

sphere
Posts: 629
Joined: Sat Sep 27, 2014 10:32 am
Location: the Netherlands

Re: Saving data on Android

Post by sphere » Mon Jan 15, 2018 8:16 pm

i must have misread the first topic...
Please, stop quoting things my brother did not say. Thank you. Hans Zweistein.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4505
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Saving data on Android

Post by jacque » Tue Jan 16, 2018 7:40 pm

The cache folder is not permanent. Data will remain until the OS does housekeeping, or the user clears the cache to regain disk space. You can't rely on those files being there, the cache is meant for temporary data. The documents folder is permanent and never cleared until the user uninstalls the app.

Both the cache and documents folders are stored inside the app sandbox and cannot be accessed normally. Those files are not displayed by a file manager and cannot be seen or edited by users. The exception is if the device has been rooted. If so, all files are available. But most users do not have rooted phones so the documents folder is as secure as it gets.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

DolphinTech
Posts: 10
Joined: Thu Dec 28, 2017 1:33 pm

Re: Saving data on Android

Post by DolphinTech » Sun Jan 21, 2018 12:29 pm

Thank you. I will consider using the "documents" folder.

-P.S. how do I close a topic?

Post Reply

Return to “Android Deployment”