3 Replies Latest reply on Jan 1, 2008 4:36 PM by weebib

    Loading image from database

    weebib
      Hi all,

      The structure of my application is :
      - a flex client
      - a tomcat server
      - a mysql database
      I use hibernate to retrieve my objects from the database.

      I'm trying to have the flex client display images loaded at runtime from the database. Those images are stored as byte arrays (byte[]) and were produced in java with the following type of code : ImageIO.write(myBufferedImage, "PNG", myByteArrayOutputStream); so those byte arrays pretty much represent the content of a png image file.

      Is there any chance I might be able to produce a BitmapData out of the ByteArray instance I'll retrieve on the flex side or should I rely on some kind of a decoder ? Another solution could be to rewrite my storing mechanism but I'm trying, as much as possible, to avoid having to modify my current database content ;)

      Thanks in advance,

      Pierre.
        • 1. Re: Loading image from database
          atta707 Level 2
          Yes. Sure, this should work. Have you tried it? Are you having any problems?

          Easiest check would to start off with <Image> component and set it's url to the servlet that's pumping image bytes and see if it displays it right.

          ATTA
          • 2. Re: Loading image from database
            weebib Level 1
            Thanks for the quick response.

            quote:

            Yes. Sure, this should work. Have you tried it? Are you having any problems?

            Actually, I haven't.. yet (I'm currently migrating this project from echo2 to flex and I haven't reached the point where I can really test anything). I should be able to create a prototype quickly though ;) .

            quote:

            Easiest check would to start off with <Image> component and set it's url to the servlet that's pumping image bytes and see if it displays it right. ATTA

            Good idea... Thanks for the pointer. My first intention was to directly use the ByteArray retrieved through FDS to create some kind of an image, client side but, as you mentioned Image, I read through the code of both this class and its parent class SWFLoader. It turns out that this class relies on the use of a Loader (couldn't find the source ;( ). It's kind of a pity that there should be no obvious means to directly feed the Image instance with the content (or the Loader, if that's where the PNG decoding occurs) through a ByteArray format. Anyway, your suggestion of creating a Servlet to retrieve the image content is indeed a good idea. I should be able to store some kind of an image id in my object that I could use as a parameter of the servlet's url. So much for trying to leave my current database intact but I can't think of any other way, considering my very small current knowledge of the whole flex framework.

            Thanks again for the tip. I'll come back later on with the results of my tests...

            Pierre.
            • 3. Re: Loading image from database
              weebib Level 1
              So, for full disclosure, I made it work thanks to a code snipet I found at :
              http://blog.paranoidferret.com/index.php/2007/12/13/flex-snippet-duplicating-a-vector-movi eclip-image/

              I was thus able to load the image's content out of the ByteArray that were automatically retrieved through FDS / Hibernate / mysql. So, in the end, there was no need for a dedicated servlet.

              Pierre.