Tag Archives: file location

Loading external files with Adobe AIR

When creating Adobe AIR applications, you have the option of compiling resources into the output file. Accessing the location of the file in AIR can be slightly tricky however, and is not as simple as it is in Flash Player when run locally. To access a local file in Flash Player, you can normally make a new URLRequest() to the location of the file and it will work. In AIR, you have to use the File.as class to access the file location relative to a location within the operating system. While researching this topic, the first Google result was a post by Erik Phansen who wrote about the

file.documentsDirectory.resolvePath('eamplefileURL.jpg') 

method, and how it returns a location relative to the users Documents folder on their machine as a String. However, we think there is a more useful method which is

 file.applicationDirectory.resolvePath('examplefileURL.xml'); 

which returns a path relative to the location of the deployed application. For example, you can use TweenMax library to load in multiple items easily, using the following code:

 
var file:File = File.applicationDirectory.resolvePath("AppData.xml");
queue = new LoaderMax({name:"xmlQueue", onProgress:XMLprogressHandler, onComplete:XMLcompleteHandler, onError:XMLerrorHandler});
queue.append( new XMLLoader(file.url, {name:"appData"}) );
queue.load();

You need to write the missing event handlers, but you can easily add multiple items to the queue to extend it. This is great for creating AIR applications where you want to have text, images, video or other configuration that needs to be easily be adjusted post compilation.