Page 1 of 1
How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 6:42 pm
by UrosDobricic
Viewed 4 times
0
i would be very glad if somebody help me about my problem. I'm writin an app in Livecode, and so far everything working fine.
Now i want to save the data a user created in "documents" on the users phone.
I have 5, but probably there are going to be 8 fields with different data. I wanted to safe it to a text file, so the user can upload the data into the app again if he needs this profile.
the field at the bottom is the fileName field.
I attached a screenshot of my app so far.
If the users clicks on "speichern" then it has to save the data from the fields into a variable. My thought was to save the data in 5 lines, so when I'm uploading i can just read from line to line into the fields.
Here´s my code I've written so far:
Code: Select all
on hiliteChanged
if auswahl = "speichern" then
open file specialFolderPath("documents") & "/" & field "fileName" for write
put field "kapm" into line 2 of tData
put field "hauptmm" into line 3 of tData
put field "schlagm" into line 4 of tData
put field "schlagmm" into line 5 of tData
write tData to file specialFolderPath("documents") & "/" & field "fileName"
close file specialFolderPath("desktop") & "/" & field "fileName"
end if
end hiliteChanged
I'm using the header nav as you can see on the screenshot, that's why I´m calling with hiliteChanged
I tried this code, but it does not work.
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 6:56 pm
by Klaus
Hi Uros,
welcome to the forum!
Some problems in your code:
Code: Select all
on hiliteChanged
## What did the user click on?
put the hilitedItemName of me into tAuswahl
## User clicked on "Speichern":
if tAuswahl = "speichern" then
## Get used to the shorter URL syntax and store the complete pathname only once in a variable:
put specialFolderPath("documents") & "/" & field "fileName" into tFile
put field "kapm" into line 2 of tData
put field "hauptmm" into line 3 of tData
put field "schlagm" into line 4 of tData
put field "schlagmm" into line 5 of tData
## You are adressing two differnet files here!
## DOCUMENTS
## write tData to file specialFolderPath("documents") & "/" & field "fileName"
## DESKTOP
## close file specialFolderPath("desktop") & "/" & field "fileName"
## And ALWAYS use parens when concatenating pathnames and objectnames etc:
## write tData to file (specialFolderPath("documents") & "/" & field "fileName")
## close file (specialFolderPath("desktop") & "/" & field "fileName")
## Short URL syntax, a ONE-liner :-)
put tData into url("file:" & tFile)
end if
end hiliteChanged
Best
Klaus
P.S.
There is also a german LC forum here:
http://www.livecode-blog.de/forums/foru ... ode-forum/
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 7:23 pm
by UrosDobricic
Hi Klaus,
thanks for your fast reply.
I added this code, but it did not work:
Code: Select all
on hiliteChanged
## What did the user click on?
put the hilitedItemName of me into tAuswahl
## User clicked on "Speichern":
if tAuswahl = "speichern" then
## Get used to the shorter URL syntax and store the complete pathname only once in a variable:
put specialFolderPath("documents") & "/" & field "fileName" into tFile
put field "kapm" into line 2 of tData
put field "hauptmm" into line 3 of tData
put field "schlagm" into line 4 of tData
put field "schlagmm" into line 5 of tData
put tData into url("file:" & tFile)
end if
if tAuswahl = "löschen" then
put empty into field "kapmm"
put empty into field "kapm"
put empty into field "hauptmm"
put empty into field "hauptm"
put empty into field "schlagmm"
put empty into field "schlagm"
end if
end hiliteChanged
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 7:37 pm
by Klaus
Hi Uros,
what exactly "does not work"? Do you get any error? No resulting file?
Does fld "filename" really contain a vaild filename (no extra characters like RETURN or something)?
And also check if the writing of the file was successful:
Code: Select all
...
put tData into url("file:" & tFile)
## Something went wrong, maybe we get a hint?
## THE RESULT = empty on success!
if the result <> EMPTY then
answer "Problem:" && the result
end if
...
If you see the dialog, please tell us what it says.
In the second part you are using a wrong variable, if that is what you mean!
Code: Select all
...
## if auswahl = "löschen" then
if tAuswahl = "löschen" then
...
Best
Klaus
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 7:48 pm
by UrosDobricic
Hi again,
i saw my mistake with tAuswahl.
It now say's when i run it on my PC in LiveCode "Problem: Can't open file"
When i run it on my mobile phone, it doesn't create my folder at data
and therefore it doesn't save any data.
There is no Error on my phone, when i click on speichern, then just nothing happens.
The saving is the problem, the "löschen" option is working fine.
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 8:12 pm
by Klaus
UrosDobricic wrote: ↑Wed Sep 25, 2019 7:48 pm
...
It now say's when i run it on my PC in LiveCode "Problem: Can't open file"
What is in field "filename"?
UrosDobricic wrote: ↑Wed Sep 25, 2019 7:48 pm
...
When i run it on my mobile phone, it doesn't create my folder at data and therefore it doesn't save any data.
?
There is no creation of any folder involved, -> specialfolderpath("documents") is already present.
UrosDobricic wrote: ↑Wed Sep 25, 2019 7:48 pm
There is no Error on my phone, when i click on speichern, then just nothing happens.
A runtime is NOT the IDE, so if you do not script to show errors then the script will just stop when encountering any error, so the end-user does not see anything!
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 8:19 pm
by UrosDobricic
Oh, ok. I thought it will create a folder with my Identifier of my app and paste it then in this folder.
But i just looked, there is no file with the text i wrote into the field "fileName"
I wrote into the field "fileName" Testdatei
My plan is to save the data from the fields into this Testdatei
No special characters, just text.
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 8:25 pm
by Klaus
OK, did you try to check "the result" as described above?
Maybe you can post your complete script?
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 8:32 pm
by UrosDobricic
The result is "Can't open file"
What i dont't understand is, why he is trying to open a file?
Or is he opening the file to write in it?
This is what i get when i click on the speichern button after adding your code.
My script in the menubar:
Code: Select all
on hiliteChanged
## What did the user click on?
put the hilitedItemName of me into tAuswahl
## User clicked on "Speichern":
if tAuswahl = "speichern" then
## Get used to the shorter URL syntax and store the complete pathname only once in a variable:
put specialFolderPath("documents") & "/" & field "fileName" into tFile
put field "kapm" into line 2 of tData
put field "hauptmm" into line 3 of tData
put field "schlagm" into line 4 of tData
put field "schlagmm" into line 5 of tData
put tData into url("file:" & tFile)
## Something went wrong, maybe we get a hint?
## THE RESULT = empty on success!
if the result <> EMPTY then
answer "Problem:" && the result
end if
end if
if tAuswahl = "löschen" then
put empty into field "kapmm"
put empty into field "kapm"
put empty into field "hauptmm"
put empty into field "hauptm"
put empty into field "schlagmm"
put empty into field "schlagm"
end if
end hiliteChanged
My script in my stack:
Code: Select all
on preOpenStack
if the environment is "mobile" then
set the fullscreenmode of me to "exactFit"
set the acceleratedrendering of me to true
end if
end preOpenStack
# Werte zur richtigen Berechnung als Punktwerte
# da nur mit Punkt (english) korrekt berechnet wird
function ohne_komma tZahl
replace "." with "" in tZahl
replace "," with "." in tZahl
return tZahl
end ohne_komma
function mit_komma tZahl
replace "." with "," in tZahl
return tZahl
end mit_komma
My sript at the button berechnen:
Code: Select all
local zwischenrechnung,kapmm,hauptmm,hauptkvadrat,kapkvadrat,schlagmm,schlagkvadrat
on preOpenStack
if the environment is "mobile" then
set the fullscreenmode of me to "exactFit"
set the acceleratedrendering of me to true
end if
end preOpenStack
on mouseDown
put field "kapmm" into kapmm
put ohne_komma(kapmm)^2 into kapkvadrat
put field "hauptmm" into hauptmm
put ohne_komma(hauptmm)^2 into hauptkvadrat
put kapkvadrat / hauptkvadrat into faktor
put faktor * field "kapm" into zwischenrechnung
put mit_komma(round(zwischenrechnung,2)) into field "hauptm"
end mouseDown
on mouseUp
put field "schlagmm" into schlagmm
put field "schlagm" into schlagm
put ohne_komma(schlagmm)^2 into schlagkvadrat
put hauptkvadrat / schlagkvadrat into faktor2
put (schlagm-zwischenrechnung)*faktor2 into fuellschnurm
put "Die benötigte Füllschnurlänge in Meter beträgt : " & mit_komma(round(fuellschnurm*-1,2)) into field antwort
put "Auf die Rolle passen in Meter insgesamt : "& the value of field "hauptm" into line 2 of field antwort
end mouseUp
Thats all i have for now in my app.
Thank's Klaus for helping me out

Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 8:52 pm
by Klaus
"ohne_komma", "mit_komma" déjà vue!
Hm, scripts are OK, but do you see the dialog when trying to save?
Code: Select all
...
put tData into url("file:" & tFile)
if the result <> EMPTY then
answer "Problem:" && the result
end if
...
This is the only point where it could go wrong.
And how did you check that the file is NOT getting written?
I would LC take a look and add this line right after writing the file:
Code: Select all
...
put tData into url("file:" & tFile)
if the result <> EMPTY then
answer "Problem:" && the result
end if
answer files(specialfolderpath("documents"))
...
That should display "Testdatei" and all files in your docs folder.
Re: How to save data on android from different fields into a text file with different data?
Posted: Wed Sep 25, 2019 9:02 pm
by UrosDobricic
I added this as you can see in my code above.
At my PC it opens a dialog box and says "Can't open file"
I added also this
Code: Select all
answer files(specialfolderpath("documents"))
When i click now on speichern at my phone it doesn't open anything.
Maybe, if it is ok for you, i can send you my livecode file at your email, so you can check everything?
I looked into "Interner Speicher" and then in the data folder at my phone.
There are no documents stored.
Re: How to save data on android from different fields into a text file with different data?
Posted: Thu Sep 26, 2019 8:44 am
by AxWald
Hi,
"answer files" is a command, maybe this is mixing up the call?
You might try:
Code: Select all
put tData into url("file:" & tFile)
put the result into tResult
put files(specialfolderpath("documents")) into tTheFiles
answer "Result:" && tResult & CR & tTheFiles
I'd try it in the oldfashioned way:
Code: Select all
-- we assume:
-- "myFileName" is the filename, "myData" is the content of the file
put the defaultFolder into myOldDF
set the defaultfolder to specialfolderPath("documents") & "/"
put myData into URL ("file:" & myFileName)
put the result into myResult
-- now we display what happened:
put "FilePath: " & the defaultfolder & CR & \
"FileName: " & myFileName & CR & \
"Result: " & myResult & CR & \
"Files: ==========" & CR & the files & CR & \
"Folders: ==========" & CR & the folders & CR & CR & "DefaultFolder is resetted!" into myMsg
set the defaultFolder to myOldDF
answer myMsg
I'm using the detour of "the defaultFolder" to be compatible with older versions of LC, and because I'm used to it :)
(and because I copied most of this from a working stack ...)
Have fun!
Re: How to save data on android from different fields into a text file with different data?
Posted: Thu Sep 26, 2019 10:29 am
by Klaus
FYI, it actually worked, but since the file was not easy to spot, if at all, Uros thought it didn't.
And yes, the answer files(x) was my fault. I see that two function overlap here:
answer files
and
files(the folder)
So I splitted it and we found the file is present:
Code: Select all
...
put files(specialfolderpath("documents")) into tFiles
answer tFiles
...
Re: How to save data on android from different fields into a text file with different data?
Posted: Thu Sep 26, 2019 1:25 pm
by UrosDobricic
Hi Klaus, Hi AxWald,
it actually worked. The phone just did not display anything. After adding the code above from Klaus, it showed me the files.
I also wrote the read skript, which is working fine.
Now, my second question is, can i write directly to the phone like a .txt file out of the sandbox for example in the real Documents folder of Android?
The files are now sandboxed, and as i saw, this is the way the app works.
Something like this for Windows:
0x001a: The user application data folder (e.g. "C:/Documents and settings/Fred/Application Data")
0x0023: The "all users" application data folder (e.g. "C:/Documents and settings/all users/Application Data")
0x000d: The "My Music" folder
0x000e: The "My Videos" folder
0x0027: The "My Pictures" folder
0x000a: The recycle bin