-
1. Re: Best way to save images streamed from web to device?
xTLS Jul 10, 2013 10:05 AM (in response to xTLS)Surely someone has some idea of how to approach this.
-
2. Re: Best way to save images streamed from web to device?
cmholden1 Jul 11, 2013 8:58 AM (in response to xTLS)We typically use a native extension to do this, but it is certainly possible in ActionScript.
When the file has been downloaded, we save it to the applicationStorageDirectory.
Next time, we check to see if exists locally, if so - use that and don't download.
var file:String = renameFile(_externalHttpImage);//see if we already have a copy of the image
var loader:Loader = new Loader();
var ba:ByteArray = new ByteArray();
if( fileExists(file)){
file = File.applicationStorageDirectory.resolvePath(file).nativePath;
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, local_loader_complete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorOnChargeFile);
var f:File = File.applicationStorageDirectory.resolvePath(file);if( f.exists){
var fs:FileStream;
try {
//now load the size of the file
fs = new FileStream();
fs.open(f, FileMode.READ);
fs.readBytes(ba);
// trace("READ " + fs.readUTF());
fs.close();
}
catch(e:Error){
trace( "Error Loading Image File '" +file + ": " + e.message+ "\n"+ e.getStackTrace());
}
}
loader.loadBytes(ba);
} -
3. Re: Best way to save images streamed from web to device?
xTLS Jul 17, 2013 3:52 PM (in response to cmholden1)Sounds like a good start. Thanks cmholden! What method do you use to save the image loaded from the web?
Say we use a Loader called graphicLoader to load an image. on Event.COMPLETE, we can access the image as a DisplayObject using graphicLoader.content(). Are we able to simultaneously save the image in its original format? e.g.
var bytes:ByteArray = new ByteArray();
bytes.writeObject(graphicLoader.content());
fileStream.writeBytes ( bytes, 0, bytes.length );
or do we have to load and save the image file in a different manner and then convert it to a DisplayObject?
I am struggling to find anything online that addresses this.
-
4. Re: Best way to save images streamed from web to device?
xTLS Jul 18, 2013 11:57 AM (in response to xTLS)Ah, I got it. Checking for existing local files is done as cmholden indicated. If the image isn't stored locally, we use URLStream to load the image file from the web. We can then save the file directly via the ByteArray from the URLStream, and then use our existing Loader code to load the same ByteArray as a DisplayObject. I was concerned there would be some app slowdown from saving the files but the change in performance is negligible, and of course once the images are saved on the device they load much faster the next time the app is run!

