Working with Adobe AIR FileSystem API on iPad

Recently I made some test on iPad to test how to update contents of my mobile applications.
I tried some new AIR 2 API like open default program with PDF or XLS files but I get error #3000 (privileges error).
My idea is to create a routine where I’ll be able to update all contents on my application without passing trough Apple store.
So I tried with URLStream and AIR filesystem API, everything works well.
Code is so easy, take a look here:

//RETRIEVING IMAGE FROM ANY WEBSERVER
var urlS:URLStream = new URLStream();
urlS.addEventListener(Event.COMPLETE, downloadComplete);
urlS.addEventListener(ProgressEvent.PROGRESS, showPerc);
urlS.load(new URLRequest("http://192.168.1.9:8888/oliviaWilde.jpg"))

//DOWNLOADED IMAGE BYTEARRAY AND LOADING IN LOADER
function downloadComplete(e:Event):void {

  perc_txt.text = "";
  fileData = new ByteArray();
  urlS.readBytes(fileData,0,urlS.bytesAvailable);
  bytes = urlS.bytesAvailable;
  l.loadBytes(fileData);
  l.addEventListener(MouseEvent.CLICK, saveIt);

}
//SAVE IMAGE INTO APPLICATIONSTORAGEDIRECTORY
function saveIt(e:MouseEvent):void{

  f = File.applicationStorageDirectory.resolvePath("a.jpg");
  var fs:FileStream = new FileStream();
  fs.open(f, FileMode.WRITE)
  fs.writeBytes(fileData);
  fs.close();

}

//LOAD IMAGE FROM APPLICATIONSTORAGEDIRECTORY 
function loadLocalImg(e:MouseEvent):void{

  var finalBA:ByteArray = new ByteArray();
  var fs:FileStream = new FileStream();
  fs.open(f, FileMode.READ);
  fs.readBytes(finalBA, 0, bytes);
  fs.close();
  l.loadBytes(finalBA);

}

Using applicationStorageDirectory, you can save data into your application and retrieve it with AIR API.
In the video below, you can see a little demo where I load an image from a server, I save it, I unload click on the black right button and finally I load again but from ApplicationStorageDirectory, with the black left one, instead of loading from webserver.

Advertisement

Published by

luca mezzalira

Being associated with the industry since 2004, I have lent my expertise predominantly in the solution architecture field. I have gained accolades for revolutionising the scalability of frontend architectures with micro-frontends, from increasing the efficiency of workflows, to delivering quality in products. My colleagues know me as an excellent communicator who believes in using an interactive approach for understanding and solving problems of varied scopes. I helped DAZN becoming a global streaming platform in just 5 years, now as Principal Architect at AWS, I'm helping our customers in the media and entertainment space to deliver cost-effective and scalable cloud solutions. Moreover, I'm sharing with the community the best practices to develop cloud-native architectures solving technical and organizational challenges. My core industry knowledge has been instrumental in resolving complex architectural and integration challenges. Working within the scopes of a plethora of technical roles such as tech lead, solutions architect and CTO, I have developed a precise understanding of various technicalities which has helped me in maximizing value of my company and products in my current leadership roles.

9 thoughts on “Working with Adobe AIR FileSystem API on iPad”

      1. Do you mean the data would deleted or left intact? You’re using application storage directory. I wouldn’t use that one because Adobe docs state that ‘When you uninstall an AIR application, whether the uninstaller deletes the application storage directory and its files depends on the platform.’

  1. WOW, Thank you sooo much!!

    My existing system was using Base64.encodeByteArray() to create the byteArray… Needless to say it was taking over 20 seconds per image to save and the whole system would freeze…

    I was on the verge of giving up but your method downloads and saves the same image in less than a second… You’re a life safer!

  2. Thanks so much for posting this. I am having some trouble getting it work, could you post the .fla file you created it with it? Thanks so much

  3. Great post – thanks for posting!

    Do you know if it would be possible to download a video in H.264 MP4 format and play the video while downloading it? Once the video has completed downloading it would need to be saved to local storage so in future the app would play the local version rather than the version on the remote server.

    Worse case scenario, I do not make the video available until it has 100% downloaded but it would be nice if it could play as soon as some of it has downloaded.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s