0

Please help me to retrieve image I saved as #blob in #mysql database

This is my code I used for saving the image

    JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();

    filename =f.getAbsolutePath();
    ImageIcon imageIcon = new ImageIcon(new ImageIcon(filename).getImage().getScaledInstance(lbl_img.getWidth(), lbl_img.getHeight(), Image.SCALE_DEFAULT));
    lbl_img.setIcon(imageIcon);
  try {

        File image = new File(filename);
        FileInputStream fis = new FileInputStream (image);
        ByteArrayOutputStream bos= new ByteArrayOutputStream();
        byte[] buf = new byte[1024];

        for(int readNum; (readNum=fis.read(buf))!=-1; ){

            bos.write(buf,0,readNum);
        }
        person_image=bos.toByteArray();
    }

    catch(Exception e){
        JOptionPane.showMessageDialog(null,e.getMessage());

    }

But when I try to retrieve my image using

          byte[] img = rs.getBytes("IMAGE");
          ImageIcon imageIcon = new ImageIcon(new ImageIcon(img).getImage().getScaledInstance(lbl_image.getWidth(), lbl_image.getHeight(), Image.SCALE_SMOOTH));
          lbl_image.setIcon(imageIcon);

It doesn't give any error, yet it doesn't retrieve the image. Please help me to sort out where the problem is

Til
  • 5,150
  • 13
  • 26
  • 34
  • Use `ImageIO` to write/read the bytes, for [example](https://stackoverflow.com/questions/13833089/how-to-show-an-image-from-ms-access-to-jpanel-in-java-netbeans/13833233#13833233), [example](https://stackoverflow.com/questions/20961065/converting-image-in-memory-to-a-blob/20961506#20961506), [example](https://stackoverflow.com/questions/35069359/trying-to-retrieve-both-text-and-blob-from-mysql-to-jtable/35072936#35072936), [example](https://stackoverflow.com/questions/20752432/convert-bufferedinputstream-into-image/20753089#20753089) – MadProgrammer May 15 '19 at 23:39

1 Answers1

0

Use ImageIO.read to read these bytes to BufferedImage

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32