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

Filed under: AIR, JavaScript

comments (5) Views: 5,034

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.

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

Aaah this is great stuff! Thanks! I was looking to enumerate already mounted drives, and your article pointed me to air.StorageVolumeInfo.storageVolumeInfo.getStorageVolumes(); thanks again!

Bala - November 23, 2010 03:00 am

I have a .bat file that removes all file/folders attributes and delete all .tmp files in the drive. Is it possible to open a .bat file when a USB drive is detected? thanks..

tonext - November 28, 2010 07:25 am

tonext...

AIR can only interact with native processes if you compile it as an EXE or DMG. Here's a few links to blog posts that discuss this and how to accomplish it:

Interacting with a native process

A demonstration of the NativeProcess APIs in AIR 2

AIR 2.0 Native Process - Part 1 and Part 2

andy matthews - November 28, 2010 12:47 pm

Thanks Andy, this was very helpful in getting started with this.

Jason Dean - April 21, 2011 04:53 pm

Glad it could help you out Jason.

andy matthews - April 21, 2011 09:42 pm