Writing to and Reading from a File in Android
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 58
- Joined: Mon Dec 05, 2011 5:35 pm
Writing to and Reading from a File in Android
I am testing my code using a Kindle Fire.
The following code worked for iOS with the only change being that instead of "engine", it was "documents". I don't see anything iOS-ish that would cause this to not run in Android. Do I have the default folder correct?
set the defaultFolder to specialFolderPath("engine")
writefile "prefs", thePrefs
on writefile fileTowrite,dataTowrite
set the defaultFolder to specialFolderPath("engine")
put dataTowrite into URL (("file:"&fileTowrite&".txt"))
end writefile
put readFromFile ("prefs") into thePrefs
function readFromFile whatFile
set the defaultFolder to specialFolderPath("engine")
return URL (("file:"&whatFile&".txt"))
end readFromFile
Any help is appreciated.
Thanks.
Judy
The following code worked for iOS with the only change being that instead of "engine", it was "documents". I don't see anything iOS-ish that would cause this to not run in Android. Do I have the default folder correct?
set the defaultFolder to specialFolderPath("engine")
writefile "prefs", thePrefs
on writefile fileTowrite,dataTowrite
set the defaultFolder to specialFolderPath("engine")
put dataTowrite into URL (("file:"&fileTowrite&".txt"))
end writefile
put readFromFile ("prefs") into thePrefs
function readFromFile whatFile
set the defaultFolder to specialFolderPath("engine")
return URL (("file:"&whatFile&".txt"))
end readFromFile
Any help is appreciated.
Thanks.
Judy
Re: Writing to and Reading from a File in Android
I'm not 100% sure, but I don't believe you are allowed to write to the engine folder. You can however write to documents and cache.
-
- Posts: 58
- Joined: Mon Dec 05, 2011 5:35 pm
Re: Writing to and Reading from a File in Android
You are correct. I was having some other issues earlier that caused the files to not read/write. I thought it was the "documents" setting but I was obviously wrong. 

Re: Writing to and Reading from a File in Android
Hi, Judy -
I'm wondering if you've had any luck loading and saving images on your Kindle Fire? I am able to do this easily in Livecode on my PC, but am having trouble executing these (seemingly) simple tasks when my code is transferred on to a physical Kindle device. Any pointers would be greatly appreciated!
Thank you,
John
I'm wondering if you've had any luck loading and saving images on your Kindle Fire? I am able to do this easily in Livecode on my PC, but am having trouble executing these (seemingly) simple tasks when my code is transferred on to a physical Kindle device. Any pointers would be greatly appreciated!
Thank you,
John
Lodestone Animation, Inc.
Macaroni Art for iOS
Macaroni Art for iOS
-
- Posts: 58
- Joined: Mon Dec 05, 2011 5:35 pm
Re: Writing to and Reading from a File in Android
John,
I am able to save my app on the Kindle Fire. My app has images and sounds and they transfer fine. What are you trying to do? I am working from a Mac and am an Android newbie but would be glad to help you in any way I can.
Judy
I am able to save my app on the Kindle Fire. My app has images and sounds and they transfer fine. What are you trying to do? I am working from a Mac and am an Android newbie but would be glad to help you in any way I can.
Judy
Re: Writing to and Reading from a File in Android
John's Device (Kindle) apparently does not have a "Document" folder 

-
- Posts: 58
- Joined: Mon Dec 05, 2011 5:35 pm
Re: Writing to and Reading from a File in Android
I downloaded ES File Explorer (it's free) to be able to see the file structure on my Kindle Fire. It looks like my Documents and Download folders are on the SD Card. Also, Android is case-sensitive when it comes to path and filenames. Documents.
Re: Writing to and Reading from a File in Android
Hi John,
In your example you used "foo.jpg" so I thought you would already take care of this fact.
Now did you get a snapshot or whatever to save on your Kindle?
If yes, how?
Best
Klaus
whereever this folder is -> specialfolderpath("documents") will return the correct path to this folder.jnmediaGAd195 wrote:I downloaded ES File Explorer (it's free) to be able to see the file structure on my Kindle Fire. It looks like my Documents and Download folders are on the SD Card.
Yes, didn't you know?jnmediaGAd195 wrote:Also, Android is case-sensitive when it comes to path and filenames. Documents.

In your example you used "foo.jpg" so I thought you would already take care of this fact.
Now did you get a snapshot or whatever to save on your Kindle?
If yes, how?
Best
Klaus
-
- Posts: 58
- Joined: Mon Dec 05, 2011 5:35 pm
Re: Writing to and Reading from a File in Android
I was just trying to provide a way for John to verify that the folder exists (or not).I downloaded ES File Explorer (it's free) to be able to see the file structure on my Kindle Fire. It looks like my Documents and Download folders are on the SD Card

I think it's specialFolderPath("Documents")whereever this folder is -> specialfolderpath("documents") will return the correct path to this folder.
(Documents with a capital D.)
Re: Writing to and Reading from a File in Android
NopejnmediaGAd195 wrote:I think it's specialFolderPath("Documents")
(Documents with a capital D.)

The docs list (under specialfolderpath) "documents" for iOS and ANDROID and "Documents" for the desktop.
Anyway, this is a LiveCode function and LiveCode is NOT case sensitive

-
- Posts: 58
- Joined: Mon Dec 05, 2011 5:35 pm
Re: Writing to and Reading from a File in Android
Klaus,
Thanks for the clarification. Like I said, I'm a newbie.
Judy
Thanks for the clarification. Like I said, I'm a newbie.

Judy
Re: Writing to and Reading from a File in Android
Hi, Judy & Klaus -
Wow, you've given me a lot to look at - thank you both for your quick replies! And no, as of late last night, I had not been able to save a snapshot on my Kindle Fire.
I think Klaus may be right in saying that mine didn't come with a "Documents" directory - it's sure been acting like it. 
Anyway, it's a new morning here on in the Western US, so maybe today things will go better!
I'll keep you both posted on my results. Thanks again to both of you for all of your help!
Cheers,
John
Wow, you've given me a lot to look at - thank you both for your quick replies! And no, as of late last night, I had not been able to save a snapshot on my Kindle Fire.


Anyway, it's a new morning here on in the Western US, so maybe today things will go better!

Cheers,
John
Lodestone Animation, Inc.
Macaroni Art for iOS
Macaroni Art for iOS
Re: Writing to and Reading from a File in Android
Hi John,
fingers crossed!
Best
Klaus
fingers crossed!

Best
Klaus
Re: Writing to and Reading from a File in Android
Hello again, Klaus & Judy -
I'm still plugging away at this, but I thought I'd post this sample stack to give you a better idea of what I'm trying to do.
In this example, my "Load Image" button loads the image "moon.jpg" from my PC's picture directory into the image "ImageArea" on my card.
The "Save Image" button saves a snapshot of the moon picture (along with the "mini red moon" foreground element) back into my local Pictures directory as "foo.jpg". "Clear Image" simply wipes the image "ImageArea" clean again for further testing. Again, all of this works perfectly in Livecode on my PC.
However, when I comment out my "PC only" lines of my "Load Image" and "Save Image" buttons, add the "Kindle only" lines back in, and then load this stack on to my Kindle, I get no results!
Neither the "Load Image" or "Save Image" buttons appear to work, and no errors are generated (even if I add Klaus' code to capture the error messages).
I'm going to keep at this until I get the Kindle to do my bidding, but thought I'd put this up in case either of you can sport something obvious.
Thanks again, and I'll post the answer if I do come across it!
--John
I'm still plugging away at this, but I thought I'd post this sample stack to give you a better idea of what I'm trying to do.
In this example, my "Load Image" button loads the image "moon.jpg" from my PC's picture directory into the image "ImageArea" on my card.
The "Save Image" button saves a snapshot of the moon picture (along with the "mini red moon" foreground element) back into my local Pictures directory as "foo.jpg". "Clear Image" simply wipes the image "ImageArea" clean again for further testing. Again, all of this works perfectly in Livecode on my PC.
However, when I comment out my "PC only" lines of my "Load Image" and "Save Image" buttons, add the "Kindle only" lines back in, and then load this stack on to my Kindle, I get no results!

I'm going to keep at this until I get the Kindle to do my bidding, but thought I'd put this up in case either of you can sport something obvious.

--John
- Attachments
-
- KindleImageTestStack.zip
- Kindle Image Test Stack
- (86.95 KiB) Downloaded 329 times
Lodestone Animation, Inc.
Macaroni Art for iOS
Macaroni Art for iOS
Re: Writing to and Reading from a File in Android
A few things I noted.
First, your load button loads the image "Moon.jpg" its named moon.jpg (lower case).
Second, you have to have some way to get the "moon.jpg" file onto the kindle to begin with. One way to do this is to use the "copy files" pane of the standalone settings, and add moon.jpg from there. This way it will be bundled up into the apk and sent along with the app when its installed.
This brings up the next thing. moon.jpg will be in the specialfolderpath("engine") folder not documents. You can read from "engine" you just can't write to it, so this should be fine.
So, if you have added moon.jpg to the copy files pane and installed the apk, your load button should be pretty simple.
For the save button, if you are just wanting to export a snapshot of the image area you should be able to do it like so:
The code for the 2 buttons (load and unload) follow. I removed the desktop stuff but it seems you have a decent handle on the desktop side so I left it out to simplify.
For the load button:
For the save button:
First, your load button loads the image "Moon.jpg" its named moon.jpg (lower case).
Second, you have to have some way to get the "moon.jpg" file onto the kindle to begin with. One way to do this is to use the "copy files" pane of the standalone settings, and add moon.jpg from there. This way it will be bundled up into the apk and sent along with the app when its installed.
This brings up the next thing. moon.jpg will be in the specialfolderpath("engine") folder not documents. You can read from "engine" you just can't write to it, so this should be fine.
So, if you have added moon.jpg to the copy files pane and installed the apk, your load button should be pretty simple.
Code: Select all
set the filename of image "imageArea" to specialfolderpath("engine") & "/moon.jpg" -- grab it from the engine folder
Code: Select all
export snapshot from image "ImageArea" to file myExportFile as JPEG
For the load button:
Code: Select all
on mouseUp
if the environment is "mobile" then
## If the copy files pane of the standalone settings was used then
## the moon.jpg file is in the engine folder as part of the apk
put specialfolderpath("engine") & "/moon.jpg" into myLoadFile
## set the image filename to the path of the file
set the filename of image "ImageArea" to myLoadFile
end if
end mouseUp
Code: Select all
on mouseUp
if the environment is "mobile" then -- just did this so that you know how to better separate different environment specific code
answer question "Save Image as foo.jpg?" with "No" or "Yes" titled "Save Image as foo.jpg?"
if it is "Yes" then
put specialfolderpath("documents") & "/foo.jpg" into myExportFile
export snapshot from image "ImageArea" to file myExportFile as JPEG -- this is easier that building a rect.
## also rather than get the topleft of this and the bottomright of that of the same object, you can
## just get the rect of the object directly. (so it could have been "export snapshot from rect (the rect of image "imageArea") to file.....)
## This is just informational. It will pop up an answer "did it save?" with either true or false depending on if the file is there or not.
answer information "Did it save? " & (there is a file myExportFile)
end if
end if
end mouseUp