I am trying to retrieve a blob(2) from my newly created embedded database and assign it to the image icon of a jlable (and of course save it back to the database if the image has been changed or altered).
I found the following code snippit from another site but am having probs getting it to work....

Blob blob1 = rs.getBlob("IMAGE");
IMAGE.setIcon(new ImageIcon(ByteStreams.toByteArray(blob1.getBinaryStream())));

Blob blob2 = rs.getBlob("POG");
POG.setIcon(new ImageIcon(ByteStreams.toByteArray(blob2.getBinaryStream())));

I am getting the error of cannot find symbol on ByteStreams
I know that I am missing something, ie defining the varible ByteStreams, but dont understand what is for or where/how to get the value for it.
Note: the images will vary in size from one to another.

recomendations to updating the database would be helpful as well....

Recommended Answers

All 5 Replies

ByteStreams looks like a Google Java class.
I haven't tried this, but maybe you could try ImageIO.read(blob2.getBinaryStream()) to get the image then pass that to an ImageIcon constructor.

Is there something like that that works in reverse.... ie going from the image icon to the database blob?

maybe something like rs.updateBlob((ImageIcon) IMAGE.getIcon(), null);

(Know that this wont work)

MikeWyatt: if there weren't, he wouldn't be able to get the image as blob from the db, now would he?
just google 'write image as blob java'. that'll give you enough of examples/tutorials.
but considering this is a different question, perhaps you should've started a new thread.

Did you get a solution to inputting from the blob? If so, how did that work? (ps It would be polite to let us know if you found a solution and, if so, what it was.)
Can you reverse it, eg write the Image to a ByteArrayOutputStream, then use that array in a call to the blob's setBytes method?

  • isn't logics for Input/OutputStream better for FileIO from database, maybe doesn't matted, but

  • you have to start to loading Image/BufferedImage, then to covert to ImageIcon/Icon (by test if is instanceof ImageIcon/Icon before)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.