Array

Caching Assets

Must Read

Begin a Debt Free Life Through Secured Debt Consolidation Loans

Incurring debts is no strange happening to modern day people. Rising standards of living and pressure to maintain it...

Affiliate Marketing Guide – The benefits of being an affiliate marketer

Affiliate Marketing has almost no production costs. It is therefore great for people who want to work from home...

Integrating Debts For You – Secured Debt Consolidation Loan

Loan market is expanding as people are becoming more dependent on loans for getting their wishes fulfilled. This is...
Admin
test

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:

[code]

import flash.display.Loader;
import flash.net.URLRequest;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
var stream:FileStream;
var fileName:String;
var file:File;
var urlLoader:URLLoader;
var url:String = “http://www.v-ro.com/cat.jpeg”;
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);
addChild(event.currentTarget.content);
}
function onRemote(event:Event):void {
event.target.removeEventListener(Event.COMPLETE, onRemote);
var byteArray:ByteArray = event.target.data as ByteArray;
stream.open(file, FileMode.WRITE);
stream.writeBytes(data, 0, data.length);
stream.close();
var loader:Loader = new Loader();
loader.loadBytes(byteArray);
addChild(loader);
}

[/code]

Previous articleAsynchronous Bitmap Decoding
Next articleComponents

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits, even though it will have...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

More Articles Like This