Simple Drag and Drop into AIR
Drag and Drop from the desktop to an AIR application is one of the new features of AIR.
Basically, I add the event listeners to the application to handle the drag events. Then when a file is dropped onto the application, I check the file extension and then handle it if is an image file or throw an alert if not. You can certainly update the switch statement to handle additional file extensions differently.
Dragging In

After Drop

Here is some sample code:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
- creationComplete="init()">
- <mx:Script>
- <![CDATA[
- import mx.controls.Alert;
- import mx.controls.Image;
- import flash.filesystem.File;
- private function init():void{
- this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
- this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
- }
- private function onDragIn(event:NativeDragEvent):void{
- NativeDragManager.acceptDragDrop(this);
- }
- private function onDrop(event:NativeDragEvent):void{
- var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
- for each (var file:File in dropfiles){
- switch (file.extension.toLowerCase()){
- case "png" :
- addImage(file.nativePath);
- break;
- case "jpg" :
- addImage(file.nativePath);
- break;
- case "jpeg" :
- addImage(file.nativePath);
- break;
- case "gif" :
- addImage(file.nativePath);
- break;
- default:
- Alert.show("Unmapped Extension");
- }
- }
- }
- private function addImage(nativePath:String):void{
- var i:Image = new Image();
- if(Capabilities.os.search("Mac") >= 0){
- i.source = "file://" + nativePath;
- } else {
- i.source = nativePath;
- }
- this.addChild(i);
- }
- ]]>
- </mx:Script>
- </mx:WindowedApplication>



June 12th, 2008 at 7:47 pm
using the following to test the file extension would shorten your code a bit:
if ( “”.indexOf( “”) > -1) { addImage(file.nativePath);
}
June 12th, 2008 at 7:50 pm
hmm… your comment system removed the good stuff…