Detecting External Storage Devices with Adobe AIR, HTML and JavaScript - The StorageVolumeChangeEvent

Phillip Senn over on the AIR with HTML and JavaScript Google Group recently asked "How to use the StorageVolumeChangeEvent". Instead of just handing over a few lines of code, or pointing him to the documentation, I thought I'd take some time and write up an AIR project that you can download and use as reference. The ability to detect external storage devices is a feature released in AIR 2.0, and this article will show you how to utilize it in your applications. Download the full source code, then import the project into ColdFusion Builder, or Aptana. Since the code is fairly short, let's look at the whole thing, then I'll call out specifics.

When you run this app, you should see something like this:

While the application is running, insert any writeable external storage device, such as a thumb drive, or external hard drive, and you should see something like this:

What allows us to snoop into the file system of the hard drive? It's the StorageVolumeChangeEvent event found in the StorageVolumeInfo class. That's shorthand for saying AIR allows us to be notified when someone plugs in a USB stick, and we can then go digging into the file system to display it's contents. Lets highlight the important parts.

These lines of code are where the magic happens. All you need to do is listen for the correct event, then when it's announced, you can act upon it. The StorageVolumeChangeEvent carries along with it all the information about the announced device that you'll need to determine the name, remaining disk space, it's system path, and even the contents of the drive itself.

I hope this shows you just how easy it is to develop AIR applications with JavaScript that offer advanced functionality. As always, stay tuned for more.