Caching Assets

Must Read

Just post!

If your application uses dynamic downloaded assets, similar to a Facebook application displaying your contacts picture, consider caching the assets on your device. Doing so will prevent the overhead of a download every time your application initializes.

Save this technique for small files so that you don’t abuse the user’s storage capacity.

Here, we are looking at the name of the file as part of a url. Check if the file already exists on the SD card. If it does, load it locally. If it does not, get the file from a remote server and save it to the device:


import flash.display.Loader;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
var stream:FileStream;
var fileName:String;
var file:File;
var urlLoader:URLLoader;
var url:String = “”;
fileName = new String(url).split(“/”).pop(); // cat.jpeg
file = File.applicationStorageDirectory.resolvePath(fileName);
if (file.exists) {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLocal);
loader.load(new URLRequest(file.url);
} else {
urlLoader = new urlLoader ();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onRemote);
loader.load(new URLRequest(url));
function onLocal(event:Event):void {
event.currentTarget.removeEventListener(Event.Complete, onLocal);
function onRemote(event:Event):void {, onRemote);
var byteArray:ByteArray = as ByteArray;, FileMode.WRITE);
stream.writeBytes(data, 0, data.length);
var loader:Loader = new Loader();


Previous articleAsynchronous Bitmap Decoding
Next articleComponents
- Advertisement -

Latest News

Experts forecast upcoming burgeoning period for China’s NEV industry

China's new energy vehicle (NEV) industry is now about to step into a burgeoning period after going through the...
- Advertisement -

More Articles Like This

- Advertisement -