Dragging and Dropping images from an AIR application

In order to drag files from an AIR application, you will need to use the NativeDragManager class. The NativeDragManager class coordinates drag-and-drop operations between an AIR application and the native operating system, between two applications, or between components within a single application.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute" xmlns:local="*" width="300" height="200" viewSourceURL="srcview/index.html"> 

    <mx:Script>
        <![CDATA[
            import mx.core.UIComponent;

            private var dragItem:UIComponent;
            private var dragImg:Image;

            private function startNativeDrag(event:MouseEvent):void
            {
                dragItem = UIComponent(event.currentTarget);
                var bitmapFile:File = File.applicationDirectory;
                bitmapFile = bitmapFile.resolvePath('air_icon.png');

                var offset:Point = new Point(-event.localX, -event.localY);

                var clip:Clipboard = new Clipboard();
                clip.setData(ClipboardFormats.FILE_LIST_FORMAT, [bitmapFile]);

                var bd:BitmapData = new BitmapData(dragItem.width, dragItem.height);
                bd.draw(dragItem);

                var dragOptions:NativeDragOptions = new NativeDragOptions();
                dragOptions.allowCopy = true;
                dragOptions.allowLink = true;
                dragOptions.allowMove = false;

                dragItem.addEventListener(NativeDragEvent.NATIVE_DRAG_COMPLETE, stopNativeDrag);
                NativeDragManager.doDrag(dragItem, clip, bd, offset, dragOptions);
            } 

            private function stopNativeDrag(event:NativeDragEvent):void
            {
                dragItem.removeEventListener(NativeDragEvent.NATIVE_DRAG_COMPLETE, stopNativeDrag);
            }
        ]]>
    </mx:Script> 

    <mx:Image id="img"
        x="83" y="41"
        source="air_icon.png"
        width="98" height="98"
        mouseDown="startNativeDrag(event)"/> 

</mx:WindowedApplication>

Leave a comment