Search code examples
actionscript-3air

Saving a image locally using action script 3


Im using the bulkloader library on AIR to download an image, then using the AS3corelib im trying to save the file locally using:

var byteArray:ByteArray = jpgEncoder.encode( loader.getContent("IMG_0004.JPG") );
stream.writeBytes(byteArray);
stream.close();

But im getting this error:

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Bitmap@8349a81 to flash.utils.ByteArray.

I have no idea how to solve this.


Solution

  • The encode function wants a BitmapData object, but you're giving it a Bitmap.

    So you should just be able to do this:

    var byteArray:ByteArray = jpgEncoder.encode( Bitmap(loader.getContent("IMG_0004.JPG")).bitmapData );