5 Replies Latest reply on Sep 8, 2009 10:38 PM by Jeff Swartz

    Help With Loading an Image In AIR

    ImJake9

      I need to load an image (GIF) with AIR using File and FileStream, but can't figure out how to do it. I need to load the image into a Bitmap or BitmapData object, so I can't use the loader class. Here's what I have so far:

      import flash.display.*;
      import flash.events.*;
      import flash.filesytem.*;
      import flash.net.FileFilter;
      import flash.utils.ByteArray;
      import flash.geom.Rectangle;
      
      var file:File = File.documentsDirectory;
      var myImage:BitmapData;
      var myFilter:FileFilter = new FileFilter("GIF Image","*.gif");
      file.addEventListener(Event.SELECT,loadImage);
      file.browseForOpen("Open",[myFilter]);
      
      function loadImage(e:Event):void{
      var stream:FileStream = new FileStream();
      stream.open(file,FileMode.READ);
      var bytes:ByteArray = new ByteArray();
      stream.readBytes(bytes);
      stream.close();
      //By the way, this image is a special format and will always be 160 by 160 pixels.
      myImage = new BitmapData(160,160);
      bytes.position = 0;
      myImage.setPixels(new Rectangle(0,0,160,160),bytes);
      addChild(new Bitmap(myImage));
      }
      

      However, this never works. I always get this error:

      Error #2030: End of file was encountered.

      I don't quite understand why. Help would be much appreciated.

        • 1. Re: Help With Loading an Image In AIR
          Jeff Swartz Level 3

          You should use a Loader object, not a FileStream object, as in the following:

           

          function loadImage(e:Event):void
          {
              var loader:Loader = new Loader();
              var urlReq:URLRequest = new URLRequest(file.url);
              addChild(loader);
          }
          • 2. Re: Help With Loading an Image In AIR
            ImJake9 Level 1

            I already know how to do that, but I specifically stated in my post that I can't use the Loader class. The reason for that is that my program is an image manipulation program, and I need to load the data into a BitmapData object. However, since I originally made my post, I have found some more information. I used trace() to find out the length of the ByteArray I was loading the data into, and then the position after trying to create the BitmapData object, like so:

            import flash.display.*;
            import flash.events.*;
            import flash.filesytem.*;
            import flash.net.FileFilter;
            import flash.utils.ByteArray;
            import flash.geom.Rectangle;
            import flash.errors.EOFError;
            
            var file:File = File.documentsDirectory;
            var myImage:BitmapData;
            var myFilter:FileFilter = new FileFilter("GIF Image","*.gif");
            file.addEventListener(Event.SELECT,loadImage);
            file.browseForOpen("Open",[myFilter]);
            
            function loadImage(e:Event):void{
                 var stream:FileStream = new FileStream();
                 stream.open(file,FileMode.READ);
                 var bytes:ByteArray = new ByteArray();
                 stream.readBytes(bytes);
                 stream.close();
                 trace(bytes.length); //Outputs: 4631
                 //By the way, this image is a special format and will always be 160 by 160 pixels.
                 myImage = new BitmapData(160,160);
                 bytes.position = 0;
                 try{
                      myImage.setPixels(new Rectangle(0,0,160,160),bytes);
                 } catch(e:EOFError){
                      trace(bytes.position); //Outputs: 4628
                 }
                 addChild(new Bitmap(myImage));
            }
            

            As the comments show, though the ByteArray is 4631 bytes, it is only read up until byte number 4628. Then it throws an error. Any idea what's going on?

            • 3. Re: Help With Loading an Image In AIR
              Jeff Swartz Level 3

              Even if you use the Loader class (which you should) you can manipulate the bitmap data. Consider the following:


              var file:File = File.documentsDirectory;
              var myImage:BitmapData;
              var myFilter:FileFilter = new FileFilter("JPEG","*.jpg");
              file.addEventListener(Event.SELECT,loadImage);
              file.browseForOpen("Open",[myFilter]);

               

              function loadImage(e:Event):void
              {
                  var loader:Loader = new Loader();
                  var urlReq:URLRequest = new URLRequest(file.url);
                  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
                  loader.load(urlReq);
              }

               

              function loaded(e:Event):void
              {
                  var bmp:Bitmap = e.target.content as Bitmap;
                  myImage = bmp.bitmapData;
                  trace(myImage.width, myImage.height);
              }

               

              This code does not put the loaded bitmap on the stage (although you could), and it gives you access to the bitmap data that you want to get.

              • 4. Re: Help With Loading an Image In AIR
                ImJake9 Level 1

                Thank you for that. I hadn't remembered the content property of a Loader object. Sorry for my stupidity!

                My program is working beautifully. Still, I wonder why that FileStream system wasn't working... it seems that it should. Anyways, this is better.

                 

                Thanks,

                Jake

                • 5. Re: Help With Loading an Image In AIR
                  Jeff Swartz Level 3

                  You were attempting to load GIF-formated bytes into a BitmapData object directly. This would not work. You would need use an algorithm to convert the GIF-formatted bytes into the format that the setPixel() method requires -- 32-bit ARGB pixel values, uncompressed.