5 Replies Latest reply on Mar 14, 2010 3:59 PM by Francisc


    Francisc Level 3



      Can any type of file - image, sound, video, document etc - be `turned into` a ByteArray variable?


      If so, how?


      Thank you!

        • 1. Re: Bite[my]Array

          not sure about the others but here's how you create image snapshot ByteArray:


          <?xml version="1.0" encoding="utf-8"?>


          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                          width="500" height="500"
                          layout="vertical" xmlns:display="flash.display.*">
                      import mx.graphics.codec.JPEGEncoder;
                      import mx.graphics.codec.PNGEncoder;
                      import mx.graphics.ImageSnapshot;
                      private var loaderPng:Loader;
                      private var loaderJpg:Loader;
                      private var byteArray:ByteArray;
                      private function createByteArray():void
                          var bitmapData:BitmapData = new BitmapData(image.width, image.height);
                          bitmapData = ImageSnapshot.captureBitmapData(image);
                          var pngEncoder:PNGEncoder = new PNGEncoder();
                          byteArray = pngEncoder.encode(bitmapData);
                          loaderPng = new Loader();
                          loaderPng.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompletePng);
                          var jpegEncoder:JPEGEncoder = new JPEGEncoder();
                          byteArray = jpegEncoder.encode(bitmapData);
                          loaderJpg = new Loader();
                          loaderJpg.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteJpg);
                      private function onCompletePng(event:Event):void
                          outputPng.source = loaderPng.content;
                      private function onCompleteJpg(event:Event):void
                          outputJpg.source = loaderJpg.content;
              <mx:Image id="image" source="http://www.google.com/intl/en_ALL/images/logo.gif"/>
              <mx:Button label="create ByteArray" click="createByteArray()"/>
              <mx:Image id="outputPng"/>
              <mx:Image id="outputJpg"/>



          I guess you can experiment with others, let us know.




          1 person found this helpful
          • 2. Re: Bite[my]Array
            msakrejda Level 4

            If by "turned into" you mean "embedded as", then yes. You just need to set the mimeType attribute of the [Embed] tag to 'application/octet-stream'. Getting something sensible out of those bytes once you're in Flex can be trickier. If it's a plain text document, you can just create an instance of the embedded Class (which will be a special ByteArray subclass) and use ByteArray's readUTFBytes() to read out its contents.


            For unsupported image, video, and document types, you'd need to decode the format yourself, which is probably no small undertaking (not to mention that you almost certainly couldn't do anything useful with the video formats).

            1 person found this helpful
            • 3. Re: Bite[my]Array
              Francisc Level 3

              Hm, yeah, video would be tricky, I was asking in general though.

              How can I make a user selected file (Image, Sound, Document etc) a ByteArray var?

              • 4. Re: Bite[my]Array
                Darrell Loverin Level 4

                You could read it into a ByteArray with URLLoader assuming you passed the security checks.




                • 5. Re: Bite[my]Array
                  Francisc Level 3

                  Good idea! Thank you, Darrell!