views:

963

answers:

1

Given: A Flex TileList with the following event:

<mx:nativeDragDrop>
  <![CDATA[
    if(event.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) {
      var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

      for each(var file:File in files)
      {
        // file.data is null here!
      }

      this.listData.refresh();
    }
  ]]>
</mx:nativeDragDrop>

I am trying to create a list of thumbnails from jpegs that I drag into this TileList. Image.source can use the url to show the image, but I need to scale the image down first (hi rez photos) I already have the scaling part done except that I need BitmapData from the file and it has null for file.data.

ALSO, I have tried this:

var x:URLRequest = new URLRequest(value.file.url); // this is a local file (e.g. file:///C:/somefile.jpg)
var b:Bitmap = new Bitmap(x.data as BitmapData);

data is ALSO null! So frustrating. Any help would be appreciated.

A: 

I assume this is a part of an AIR application. (Accessing the clipboard from a plain Flex app is not possible.)

I have no experience with AIR, but your second code block is clearly wrong. An URLRequest instance does nothing in itself, it is but a static object storing the request details. In order to fetch the data from that URL, you need to create a Loader, and pass the request to that loader like this:

var req:URLRequest = new URLRequest(value.file.url); // this is a local file (e.g. file:///C:/somefile.jpg)
var ldr:Loader = new Loader();
ldr.addEventListener(Event.COMPLETE, function(event:Event):void {
   var b:Bitmap = event.target.content as Bitmap;
});
ldr.load(req);

Of course, you'd have to fill in the Event.COMPLETE handler. Note that the Loader class can be used to load SWF and image objects, for all everything else, you'd have to use URLLoader and parse the data yourself.

Regarding the nativeDragDrop block, here's a snippet from the documentation:

Typically a handler for the nativeDragEnter or nativeDragOver event evaluates the data being dragged, along with the drag actions allowed, to determine whether an interactive object can accept a drop. To specify that an interactive object is an eligible target, the event handler must call the NativeDragManager.acceptDrop() function, passing in a reference to the object. If the user releases the mouse button over the designated object, the object becomes the drop target and dispatches the nativeDragDrop event.

Are you calling NativeDragManager.acceptDrop() properly?

David Hanak
I'm not having issues with the drg/drp functionality. It's the fact that I cant get data from a file by accessing it. Thx for the URLRequest tip. Right after I posted this, I also tried: var x:File = new File(value.file.url); // x.data still null!Could I use the Loader class in this case too?
DJTripleThreat
ok it really comes down to this. Opening the file works. I can view information about the file. However, this is still breaking:var x:File = new File(value.file.nativePath);x.load();var b:Bitmap = x.data as Bitmap; // x.data is still null!
DJTripleThreat
Like I said, I'm not an AIR programmer, but by looking at the specs, I see that here again, you'll have to add an Event.COMPLETE listener, and read the data only in the listener. Also, I'm don't think that you can cast data, a ByteArray attribute, into a Bitmap directly.
David Hanak
Looking at your original code again, it is very possible that in the nativeDragDrop handler, you also need to issue a load() call on the received file object before accessing its data attribute.
David Hanak