1 Reply Latest reply on Apr 16, 2010 6:31 AM by David_F57

    Getting jpgs from AIR.fileApplicationStorage

    richtextformat

      this is driving me crazy

       

      putting loaded jpgs into the directory is simple and works well:

       

      public function setImage (imageName:String, image:Bitmap):void
      {
          var encoder:JPEGEncoder = new JPEGEncoder(80);
          var byteArray:ByteArray = encoder.encode(image.bitmapData);
          var file:File = File.applicationStorageDirectory.resolvePath(imageName);
          var fileStream:FileStream = new FileStream();
         
          try {
              fileStream.open(file, FileMode.WRITE);
              fileStream.writeBytes(byteArray);
              fileStream.close();
             
          } catch (e:Error){
              trace('setImage ERROR:', e.message);
          }
      }

       

      getting them out again seems nigh on impossible:

       

      public function getImage (imageName:String):Bitmap
      {
          var file:File = File.applicationStorageDirectory.resolvePath(imageName);
          var bytes:ByteArray = new ByteArray();
          trace('getImage BEF', bytes.bytesAvailable);
          var stream:FileStream = new FileStream();
          stream.open(file, FileMode.READ);
          stream.readBytes(bytes, 0, stream.bytesAvailable);
          stream.close();
          trace('getImage AFT', bytes.bytesAvailable);
         
          var loader:Loader = new Loader();
          loader.addEventListener(Event.COMPLETE, onComplete)
          loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
          loader.loadBytes(bytes);
         
          var bitmapData:BitmapData = new BitmapData(175,175);
          bitmapData.draw(loader);
          var bitmap:Bitmap = new Bitmap(bitmapData);
         
          return bitmap;
         
          trace('getImage -----------------------');
      }

       

      private function onComplete (event:Event):void
      {
          trace('onComplete', event.target);
      }

       

      private function onError (event:IOError):void
      {
          trace('onError', event.message);
      }

       

      running two stored images through this retrieval method gives the following trace output:

       

      getImage BEF, 0
      getImage AFT, 6760
      getImage
      -----------------------
      getImage BEF, 0
      getImage AFT, 7039
      getImage
      -----------------------

       

      so clearly the bytearray data is being loaded, but the onComplete and onError methods are NEVER fired and the returned Bitmap is just a white square. the files in question definitely are jpgs.


      what am I doing wrong?