Calendar

February 2008
M T W T F S S
« Jan   Mar »
 123
45678910
11121314151617
18192021222324
2526272829  

Tag Cloud

Categories

Archives

Highest Rated

Most Viewed

Recent Posts

Recent Comments


« AIR ActionScript / JavaScript Bridge | Main | Flex 3 is Here »

Simple Drag and Drop into AIR

By Rich Tretola | February 25, 2008Print This Post Print This Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...
717 views

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
dd1.jpg

After Drop
dd2.jpg

Here is some sample code:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"
  3.   creationComplete="init()">
  4.  
  5.     <mx:Script>
  6.       <![CDATA[
  7.         import mx.controls.Alert;
  8.         import mx.controls.Image;
  9.         import flash.filesystem.File;
  10.  
  11.         private function init():void{
  12.           this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
  13.           this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
  14.         }
  15.  
  16.         private function onDragIn(event:NativeDragEvent):void{
  17.           NativeDragManager.acceptDragDrop(this);
  18.         }
  19.  
  20.         private function onDrop(event:NativeDragEvent):void{
  21.           var dropfiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
  22.           for each (var file:File in dropfiles){
  23.             switch (file.extension.toLowerCase()){
  24.               case "png" :
  25.                 addImage(file.nativePath);
  26.                     break;
  27.               case "jpg" :
  28.                 addImage(file.nativePath);
  29.                 break;
  30.               case "jpeg" :
  31.                 addImage(file.nativePath);
  32.                 break;
  33.               case "gif" :
  34.                 addImage(file.nativePath);
  35.                 break;
  36.               default:
  37.                 Alert.show("Unmapped Extension");
  38.               }
  39.             }
  40.           }
  41.  
  42.           private function addImage(nativePath:String):void{
  43.             var i:Image = new Image();
  44.             if(Capabilities.os.search("Mac") >= 0){
  45.               i.source = "file://" + nativePath;
  46.             } else {
  47.               i.source = nativePath;
  48.             }
  49.             this.addChild(i);
  50.           }
  51.  
  52.         ]]>
  53.     </mx:Script>
  54.  
  55. </mx:WindowedApplication>

Share/Save/Bookmark

Topics: Adobe AIR |

2 Responses to “Simple Drag and Drop into AIR”

  1. mark johnson Says:
    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);
    }

  2. mark johnson Says:
    June 12th, 2008 at 7:50 pm

    hmm… your comment system removed the good stuff…

Comments