0

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.

DomingoSL
  • 14,920
  • 24
  • 99
  • 173

2 Answers2

3

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 );
Sunil D.
  • 17,983
  • 6
  • 53
  • 65
  • That gives me: Scene 1, Layer 'loader on droids', Frame 1, Line 26 1180: Call to a possibly undefined method BitMap. – DomingoSL Feb 19 '13 at 12:24
  • I made a typo, the class name is Bitmap (lowercase m). – Sunil D. Feb 19 '13 at 12:25
  • Its working but the function generates a huge ammount of lag for a 1Mb picture!, do you think this is normal??? – DomingoSL Feb 19 '13 at 12:28
  • Well, I'm not sure this is the right approach. The JPEGEncoder is for converting a bitmap into a JPEG. But your image is already a JPEG. When you do `loader.getContent` you're getting a bitmap representation of the image. So perhaps what you really want to be doing is converting that Bitmap object to a ByteArray? I'd need to google for that solution, but I'm sure it's out there. – Sunil D. Feb 19 '13 at 12:34
  • Doh, I just re-read your question. You just want to save the downloaded JPEG to disk right? I guess this is an approach that works, but it does seem like there should be another way to do that w/out having to re-encode the image. – Sunil D. Feb 19 '13 at 12:36
  • I will be very glad if you help with this, i'd already had that doubt, im downloading a jpeg file, why do i have to encode it again to save it??!! – DomingoSL Feb 19 '13 at 12:38
  • There is [another question](http://stackoverflow.com/questions/2589913/flex-3-file-download-without-urlrequest/2591626#2591626) that cover this, . You can do it with FileReference.download(). They link to the documentation for FileReference.download() and that has a full example of doing it. But it will open a dialog box asking the user to save the file. Also, on first glance at those docs, the pertinent info was hidden, I had to mouse over the "Runtimes" checkbox at the top and select modern versions of Flash/AIR to see this API. – Sunil D. Feb 19 '13 at 12:52
  • Also this [example](http://stackoverflow.com/questions/4198212/download-save-write-a-file-on-the-clients-hard-disk-using-flash-flex) (w/a very short code snippet, compared to the one in the docs). – Sunil D. Feb 19 '13 at 12:56
  • Is not the way to go cuz i need silent saves – DomingoSL Feb 19 '13 at 13:39
  • Regarding the speed, doing any operations with byte arrays is excruciatingly slow, especially when it comes to images. Unfortunately, AS3, as far as I know, does not have a convenient "save image to disk" approach that involves anything but byte arrays (after all, an image, if you open an unencrypted one in notepad, is just a giant 2D array). So, the entire process of saving and loading will be slow no matter what you do.. :( – Eternalspirit Oct 16 '13 at 02:50
1

DomingoSL, so your original prob was solved and now your problem is jpeg encoding. yes the jpeg encoder in the standard library is very slow. its the slowest encoder you could ever use. read it up on: http://www.bytearray.org/?p=775. Hint: read all the comments and youll get your answer to silent saves.

StephenNYC
  • 1,234
  • 3
  • 12
  • 29