-1

I have a byte array that comes as a result of doing a get to a method called "getFoto()". Now my question is how to convert this array of bytes into an image, to set this image to a specific JLabel.

       InputStream myInputStream = new ByteArrayInputStream(t.getFoto()); 
         BufferedImage someImage;
        try {
            someImage = ImageIO.read(myInputStream);
            Icon icon = new ImageIcon(someImage);
            portada.setIcon(icon);
        } catch (IOException ex) {
            Logger.getLogger(VeryModificarTrailers.class.getName()).log(Level.SEVERE, null, ex);
        }
        }

portada.setIcon((Icon) Imagen; is not working

Shean98
  • 13
  • 4

1 Answers1

1

A BufferedImage is not an Icon, and so casting will never magically convert it into one. Instead you need to create an ImageIcon first from the image:

BufferedImage someImage = ImageIO.read(something);
Icon icon = new ImageIcon(someImage);
someJLabel.setIcon(icon);

You can also pass the byte array directly into the ImageIcon constructor as this will work too

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373