Writing to and Reading from a File in Android

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Writing to and Reading from a File in Android

Post by jnmediaGAd195 » Sat Aug 11, 2012 8:02 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Writing to and Reading from a File in Android

Post by sturgis » Sat Aug 11, 2012 8:12 pm

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.

jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Re: Writing to and Reading from a File in Android

Post by jnmediaGAd195 » Sat Aug 11, 2012 8:52 pm

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. :)

jlally
Posts: 33
Joined: Wed Mar 21, 2012 6:48 pm
Contact:

Re: Writing to and Reading from a File in Android

Post by jlally » Mon Aug 13, 2012 9:14 am

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
Lodestone Animation, Inc.
Macaroni Art for iOS

jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Re: Writing to and Reading from a File in Android

Post by jnmediaGAd195 » Mon Aug 13, 2012 4:00 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Writing to and Reading from a File in Android

Post by Klaus » Mon Aug 13, 2012 4:14 pm

John's Device (Kindle) apparently does not have a "Document" folder 8)

jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Re: Writing to and Reading from a File in Android

Post by jnmediaGAd195 » Mon Aug 13, 2012 4:38 pm

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.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Writing to and Reading from a File in Android

Post by Klaus » Mon Aug 13, 2012 4:52 pm

Hi John,
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.
whereever this folder is -> specialfolderpath("documents") will return the correct path to this folder.
jnmediaGAd195 wrote:Also, Android is case-sensitive when it comes to path and filenames. Documents.
Yes, didn't you know? 8)
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

jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Re: Writing to and Reading from a File in Android

Post by jnmediaGAd195 » Mon Aug 13, 2012 4:57 pm

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 was just trying to provide a way for John to verify that the folder exists (or not). :)
whereever this folder is -> specialfolderpath("documents") will return the correct path to this folder.
I think it's specialFolderPath("Documents")

(Documents with a capital D.)

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Writing to and Reading from a File in Android

Post by Klaus » Mon Aug 13, 2012 5:03 pm

jnmediaGAd195 wrote:I think it's specialFolderPath("Documents")
(Documents with a capital D.)
Nope :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 8)

jnmediaGAd195
Posts: 58
Joined: Mon Dec 05, 2011 5:35 pm

Re: Writing to and Reading from a File in Android

Post by jnmediaGAd195 » Mon Aug 13, 2012 5:16 pm

Klaus,
Thanks for the clarification. Like I said, I'm a newbie. :)
Judy

jlally
Posts: 33
Joined: Wed Mar 21, 2012 6:48 pm
Contact:

Re: Writing to and Reading from a File in Android

Post by jlally » Mon Aug 13, 2012 6:45 pm

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. :P 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
Lodestone Animation, Inc.
Macaroni Art for iOS

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Writing to and Reading from a File in Android

Post by Klaus » Mon Aug 13, 2012 6:58 pm

Hi John,

fingers crossed! :)


Best

Klaus

jlally
Posts: 33
Joined: Wed Mar 21, 2012 6:48 pm
Contact:

Re: Writing to and Reading from a File in Android

Post by jlally » Mon Aug 13, 2012 8:10 pm

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! :P 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
Attachments
KindleImageTestStack.zip
Kindle Image Test Stack
(86.95 KiB) Downloaded 329 times
Lodestone Animation, Inc.
Macaroni Art for iOS

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Writing to and Reading from a File in Android

Post by sturgis » Mon Aug 13, 2012 9:20 pm

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.

Code: Select all

set the filename of image "imageArea" to specialfolderpath("engine") & "/moon.jpg" -- grab it from the engine folder
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:

Code: Select all

export snapshot from image "ImageArea" to file myExportFile as JPEG
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:

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
For the save button:

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

Post Reply