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

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 11:09 pm

Hi, Sturgis -

First off, thank you for your thorough response, your coding tips, and your clarifications. And while I did have "moon.jpg" copied on to my Kindle, it was living in the "Gallery" directory (Kindle's version of "My Pictures"), and therefore could not be found by the original "Load" code. Your suggestion to load "moon.jpg" though the Standalone Settings worked like a charm! :) Good catch on the "moon.jpg" case, too.

I'm still having trouble with the "Save" functionality, though, and suspect I am missing some critical piece of information. (I've worked with PC's for a long time, but am still new to Android, so it may be something very simple.)

The "Save" code you sent executes just fine on the physical Kindle, and even returns "true" when I execute it. However, when I look for the exported file, "foo.jpg", I cannot seem to find it anywhere. (It's neither in "Documents" nor in the "Gallery" folder, and a Windows search via USB turns up empty.) Anyway, I'm not sure if the Kindle is being coy about where it's saving my file, or if it's simply not being saved.

Sorry to drag out this thread - what I thought was going to be a simple path redirection is turning out to be a bit of a headache. :P

Thanks again,
--John
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 11:36 pm

Here, try this:

Change the script for the button that saves to the documents folder to this. When you save, it should list foo.jpg as one of the files

Code: Select all

on mouseUp
   if the environment is "mobile" then
      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
         
         ## set the defaultfolder to point to documents
         set the defaultfolder to specialfolderpath("documents")
         
         get the files -- puts the list of files in 'documents' into the variable 'it'
         answer information "The files in 'documents' are: " & it -- shows the list of files in 'documents'
      end if
   end if
end mouseUp

Knowing this, you could a) clear the image using your clear button, then b) have an additional, button that would load it from specialfolderpath("documents") & "/foo.jpg" .. If it works, you know got the export done correctly.

Also, you mentioned that you already have the image in your photo library. While I haven't tried it yet, you should look at 'mobilePickPhoto' and see if you can do what you need to with it. Since you are sandboxed you have to go through channels to get to external resources. Note also at the bottom of the dictionary entry that you have to add some checkmarks to your standalone settings to use the pickPhoto functionality.

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 » Tue Aug 14, 2012 12:49 am

Great - thanks again, Sturgis!
I'll give this a try and will let you know how it goes.
--John
Lodestone Animation, Inc.
Macaroni Art for iOS

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Writing to and Reading from a File in Android

Post by jacque » Tue Aug 14, 2012 5:05 am

However, when I look for the exported file, "foo.jpg", I cannot seem to find it anywhere.
If your Android device isn't rooted, I don't think you will see the files. They're in an area of the SD card that isn't normally accessible to users without elevated priveleges.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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 » Tue Aug 14, 2012 6:25 am

Okay, based on the code that Sturgis sent, I've been able to confirm that my Kindle is now properly reading, writing, and exporting image files within my test app! :)

However, I now realize that I should clarify that I am going to need my newly-saved jpeg's to be accessible to a user *outside* of the app. As an Android newbie, I had assumed that the files in specialfolderpath("documents") would be accessible from outside of my app. I realize now that this is (probably) not the case...

From what I've been able to gather, I believe I'm going to need to write my jpeg's to the Kindle's internal SD card in order for them to be accessible & viewable to an end user. Ideally, I'd like to save my jpeg's right into the Kindle's Picture Gallery (Kindle's version of the iPhone Camera Roll), which I think lives on the internal SD card.

Anyway, if anyone has advice on writing directly to an Android SD card (or even better, a Kindle Fire "Gallery"), I would appreciate it!

Thanks once again to Sturgis for the code samples, and to Klaus & Judy for their advice - my project hasn't moved forward quite as far as I'd hoped today, but I did learn a lot from your examples!

--John

P.S. jacque, just saw your post - thanks for clarifying about my "missing" files. Now found them, I'd just like to be able to get to them a little easier. ;)
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 » Tue Aug 14, 2012 2:14 pm

If you want to save to the image library look at mobileExportImageToAlbum which looks to be pretty easy.

EDIT: My mistake. Looks like exporting to album is a no go, I just coudl NOt make it work. If anyone can get it working on a kindle let me know.

Instead do this (it works, just checked)

Instead of building a path with specialfolderpath use "/sdcard/Pictures/foo.jpg"
so the line would be:

Code: Select all

         export snapshot from image "imageArea" to file ("/sdcard/Pictures/foo.jpg") as JPEG
Make sure you have checked the box giving you permission to write to external storage in the standalone settings.

Then go open the image gallery it should say that its loading new pictures and voila' They're available in the photo gallery library.

In fact, if you do mobilePickPhoto "library" you should be able to see the image you just added from your app.

Also, to make all this easier I added a field to your stack (list field) and on opencard put the files and folders into it.
Then added a mouseup handler to the field with this..

Code: Select all

   set the defaultfolder to (the defaultfolder & "/" & the clicktext)
   put the folders & cr into me
   put the files & cr after me
This way you can browse around your file structure by tapping names. Of course tapping a filename doesn't take you anywhere but its there so that you can actually see what files are in each folder. (this is how I first confirmed that the file was actually appearing in the gallery). Make sure you up the font size on the field a little.

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 » Tue Aug 14, 2012 9:25 pm

Thank you, Surgis - that worked!!! :)

And yes, mobileExportImageToAlbum was one of the first things I tried - not sure why it does not work on a Kindle Fire, but I had no luck with that command, either.

One small thing I should note for any future readers is that I switched back to defining a "rect" for my image capture area (as opposed to using just the image). This was the only way I could export a JPEG of my layered images (both moons in my example stack), as opposed to just the single moon of the original image area. It seemed that I also needed to "import" the snapshot first in order to define it for exporting, but this may very well be an extraneous step. :P Otherwise, everything worked great!

A big thank you once again to sturgis, jacque, Klaus, and Judy - I've really appreciated all of your help and your quick responses.
Cheers,
John

P.S. I've attached my updated test stack for anyone needing help with this issue in the future.
Attachments
KindleImageStackNew.zip
Updated Kindle Fire Image Stack
(87.48 KiB) Downloaded 348 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 » Wed Aug 15, 2012 9:49 pm

oh yep. If you need the capture to snap for more objects than 1 you do need to define the rect and do it that way. Glad its all working for you now!

pkocsis
Posts: 105
Joined: Sat Apr 15, 2006 7:20 am

Re: Writing to and Reading from a File in Android

Post by pkocsis » Sat May 25, 2013 8:48 am

Just thought I would post some additional information to those that might come across this topic.

See my initial post here that explains a problem with the "export image" workaround to (the seemingly not-working) mobileExportImageToAlbum command.

As detailed in my post, on my Nexus S (4.1.1), "export image" to a full path does indeed write a file, but the file is invisible to the android "gallery" because the android OS is not being notified that there was a change and does NOT rescan the Pictures folder. The only way I found to force android to rescan is to either re-boot the device, or connect the device to USB and simply access the folder....once either of those two actions are taken, the "export(ed) image" can indeed be seen via android's "gallery". I assume that if mobileExportImageToAlbum was working, that command would send the proper notification to the android OS that a "re-scan" of the Pictures folder needs to occur.

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Writing to and Reading from a File in Android

Post by Simon » Sat May 25, 2013 8:57 am

Hi pkocsis,
Have you filed a bug report on this?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

pkocsis
Posts: 105
Joined: Sat Apr 15, 2006 7:20 am

Re: Writing to and Reading from a File in Android

Post by pkocsis » Sat May 25, 2013 10:09 am

Hi Simon,

I have registered to file, and will do so as soon as I receive my confirmation email....

Mag
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 802
Joined: Fri Nov 16, 2012 10:51 pm

Re: Writing to and Reading from a File in Android

Post by Mag » Thu Dec 26, 2013 8:12 pm

I also I have spent several days around these problems.

As already point out, the command mobileExportImageToAlbum of LiveCode does not work, anyway the bug is already in the works by LC team http://forums.runrev.com/viewtopic.php?f=53&t=15259.
To do the rescan after save a file, I use a command of the external mergAngroid.

One of the problems I've found so far is that not all devices have the SD card so it is not easy to know in advance where to save the file.

Post Reply