简体   繁体   中英

ImageIO.read() returning a null value

I am trying to convert a byte array to a bufferedImage to display in a jLabel but the ImageIO.read() property is returning a null value and therefore a NullPonterException . What should I do?

 InputStream input = new ByteArrayInputStream(array);
    try {
     BufferedImage bufer = ImageIO.read(input);
     ImageIcon icon=new ImageIcon(new ImageIcon(bufer).getImage().getScaledInstance(jLabel3.getWidth(), jLabel3.getHeight(), Image.SCALE_SMOOTH));
       
        jLabel3.setIcon(icon);
    } catch (IOException ex) {
        Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
    }`

According to the javadoc , the read(InputStream) method...

"Returns a BufferedImage as the result of decoding a supplied InputStream with an ImageReader chosen automatically from among those currently registered. The InputStream is wrapped in an ImageInputStream . If no registered ImageReader claims to be able to read the resulting stream, null is returned. "

It is most likely that the last sentence explains your problem.


What should I do?

So your approach to solving this would be:

  1. Check that the contents of array is what you expect it to be.
  2. Determine what kind of image format it is, and that it is correctly represented. For example, if the image was stored in a database or sent in a network request, make sure that it hasn't gotten mangled in the process.
  3. Check that it is a supported image format; ie one that there should be a registered ImageReader class for.

Thanks for helping me to solve the problem I going to post the response here to help other.

1.The queries to the database (postgresql) must be preparedStatement because if you are saving an image converted to byte [] this declaration gives you a setBinaryStream functionality and when you retrieve it and add it in a byte[] nothing changes

   ////This way save the image and his path (the last is optional)
    JFileChooser f = new JFileChooser();
    f.showOpenDialog(null);
    File file = f.getSelectedFile();
    FileInputStream s = null;
    String path = file.getAbsolutePath();
 try {
    s = new FileInputStream(file);
            Conexion();
            PreparedStatement pq = conexion.prepareStatement("INSERT INTO prueba(foto, cam) VALUES (?, ?);");
            pq.setBinaryStream(1, s, (int) file.length());
            pq.setString(2, path);
            pq.executeUpdate();
            s.close();
      } catch (ClassNotFoundException ex) {
            Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
        }



   /////This way retrive the info
   byte[] array = null;
   String photopath = "";
   try {
        Conexion();
        PreparedStatement p = conexion.prepareStatement("SELECT foto, cam FROM prueba;");
        ResultSet sq = p.executeQuery();
        while (sq.next()) {
            array = sq.getBytes("foto");
            photopath = sq.getString("cam");
            //jLabel3.setIcon(new ImageIcon(array));

            break;
        }
        sq.close();
        p.close();
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(Add.class.getName()).log(Level.SEVERE, null, ex);
    }

    ImageIcon icon=new ImageIcon(array);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM