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.

Published by

lucamezzalira

I’m the VP of Architecture at DAZN with more than 15 years of experience, a Google Developer Expert on Web Technologies and the London Javascript community Manager I had the chance to work on cutting-edge projects for mobile, desktop, web, TVs, set top boxes and embedded devices. I am currently managing DAZN, a sports video platform based on the cloud with millions of users that are watching live and on-demand contents. I'm the author of Front-End Reactive Architectures published by APress: https://goo.gl/ywAmsx I think the best way to use any programming language is mastering their models, that’s why I spent a lot of time studying and researching on topics like OOP, Functional and Reactive programming. In my spare time, I wrote for national and international technical magazines and editors, I'm also a technical reviewer for APress, Packt Publishing, Pragmatic Bookshelf and O'Reilly. I was speaker at: O'Reilly media webinars, O'Reilly Software Architecture (San Francisco & London), O'Reilly Fluent (San Jose), O'Reilly Oscon (London), Google Developers Summit (Krakow), Google DevFest (London), Frontend Devs Love (Amsterdam), Voxxed Days (Belgrad & Bristol), JeffConf (Milan), International Javascript Conference (Munich & London), JS Poland (Warsaw), Code Europe (Wroclaw), JSDay (Verona), CybercomDev (Łódź), Jazoon Conference (Bern), JDays (Göteborg), Codemotion (Milan), FullStack Conference (London), React London UG (London), Scrum Gathering (Prague), Agile Cymru (Cardiff), Scotch on the rocks (Edinburgh & London), 360Max (San Francisco), PyCon (Florence), Lean Kanban Conference (London), Adobe Creative Suite CS 5.5 - Launch event (Milan), Mobile World Congress (Barcelona)

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 to David Cancel 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 )

Google photo

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

Twitter picture

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

Facebook photo

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

Connecting to %s