Storing Images


What is the best way to store images?

As external files? In the database as blobs? Are blobs supported?


i use odb to store images, in a complex, persistent uml model based on Eclipse EMF/UML2.

to persist the images, i just store an array of bytes (byte[]) representing the encoded images.  i.e. a class which has a field of type byte array.  to turn the bytes into an image, i use a UI toolkit called HCIL Jazz, which knows how to reconstitute images from bytes.  another way is to use something like a PNGImageDecoder directly.  this approach works well for me, but I'm guessing there's an easier way.

so, yes, the equivalent of blobs (byte arrays) are supported well.


That is correct. Images can be stored in the database as byte[] fields. byte[] is a supported persistable type in both JPA and JDO and can represent a blob.

The alternative is to keep the images as external files and keep only their path/name in the database. This may simplify using the images (e.g. when serving images in web applications), but complicates database maintenance (e.g. backup).

Using external files might improve performance for very large files (e.g. video files).

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Thanks for the info. My images are small so I will be using byte[]. Just an idea - a predefined persistable Image class could be useful. Maybe as part of a set of common entity classes for general use.


> Just an idea - a predefined persistable Image class could be useful.

(speaking for myself, i have nothing to do with objectdb apart from being a satisfied user)

a key issue for me is that such classes could not really be part of objectdb, because the JDO/JPA spec does not include them.  so, they would need to be separate from objectdb, and ideally open source, or you would have no chance of changing JDO/JPA products if required later for portability.  and that portability is one of the key advantages of JDO/JPA for me.

another issue is knowing the scope of such a library.  a basic jpeg persistable image is trivial (below), but what if you wanted to store different image types etc, you'd have to include all manner of 3rd party encoders etc.  We could then move onto audio classes etc.  Movies?  Is an object database even the right place for such things?

public class PersistentImage
    private byte[] data;
    public PersistentImage(BufferedImage image) throws IOException
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        new JPEGImageEncoderImpl(out).encode(image);
        data = out.toByteArray();        

    public Image makeImage() throws IOException
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        return new JPEGImageDecoderImpl(in).decodeAsBufferedImage();

i think what i'm trying to say is that ideally such a set of classes would be a JDO community effort rather than a JDO single vendor effort.


Post Reply

To post a reply and/or subscribe to update notifications - please login