I have some images stored in an oracle DB as blobs. I want to read them and display in a JLabel. After reading them, I have tried using ImageIO.read but it always returns null. See my code below:
Blob blob = rs.getBlob(2);
BufferedImage frontImg = ImageIO.read(blob.getBinaryStream());
lblFrontImage.setIcon(new ImageIcon(frontImg));
I am able to save the image to a file however using the following code so I know the image is valid:
Blob blob = rs.getBlob(2);
InputStream in = blob.getBinaryStream();
OutputStream out = new FileOutputStream("test.jpg");
byte[] buff = new byte[4096];
int len = 0;
while ((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
out.close();
Other ways I have tried to display the image in a JLabel
byte[] frontBytes = rs.getBytes(2);
BufferedImage frontImg = ImageIO.read(new
ByteArrayInputStream(fileContent));
lblFrontImage.setIcon(new ImageIcon(frontImg));
And
byte[] frontBytes = rs.getBytes(2);
BufferedImage image;
ByteArrayInputStream bis = new ByteArrayInputStream(frontBytes);
image = ImageIO.read(bis);
bis.close();
lblFrontImage.setIcon(new ImageIcon(image));
Also
InputStream in = blob.getBinaryStream();
image = ImageIO.read(in);
byte[] frontImgBytes = blob.getBytes(1, (int) blob.length());
System.out.println("front bytes length: ====\n" + frontImgBytes.length);
BufferedImage frontImage = ImageIO.read(new
ByteArrayInputStream(frontImgBytes));
lblFrontImage.setIcon(new ImageIcon(frontImage));
Tried a lot of ways, just keep getting java.lang.NullPointerException
. No other exception or error. Any help will be greatly appreciated.