Can't get the bitmapdata from dropped image in Flex Air AS3
When I drop an image onto my canvas I can get the nativePath to the image but not the bitmapdata which is the one I need.
In debug mode when I look into the file properties the data is set to NULL.
What am I doing wrong here? In my code file.data
doesn't give me anything.
protected function creationCompleteHandler(event:FlexEvent):void
{
this开发者_开发百科.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);
this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);
NativeDragActions.COPY;
}
private function onDrop(e:NativeDragEvent):void
{
trace("Dropped!");
var dropfiles:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
for each (var file:File in dropfiles){
switch (file.extension.toLowerCase()){
case "png" :
trace('png');
//resizeImage(file.nativePath);
break;
case "jpg" :
trace('jpg');
//resizeImage(file.nativePath);
break;
case "jpeg" :
trace('jpeg');
//resizeImage(file.nativePath);
break;
case "gif" :
resizeImage(file.nativePath);
break;
default:
Alert.show("choose an image file!");
}
}
}
first, you have to load the bytearray:
var ba:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(ba);
stream.close();
next, load the bitmap using:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
loader.loadBytes(ba);
finally, get the bitmap
private function fileLoaded(event:Event):void {
var bitmap:Bitmap = Bitmap(event.target.content);
// finally you have: bitmap.bitmapData
// cleanup
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fileLoaded);
}
精彩评论