java.lang.IllegalArgumentException: input == null! when using ImageIO.read to load image as bufferedImage

前端 未结 10 1944
予麋鹿
予麋鹿 2020-11-27 22:14

This is a question that has been asked like 100 times on this site, but I have looked at all of them and even though they all were solved, none of the solutions worked for m

相关标签:
10条回答
  • 2020-11-27 23:00

    Try using this:-

    this.icon = ImageIO.read(new FileInputStream("res/test.txt"));
    

    where res folder is present at the same level as your src folder. Also, if you notice, the slash / before the res folder name was removed.

    0 讨论(0)
  • 2020-11-27 23:01

    I was facing this error due to a bug in my code. I was trying to extract (conn.getInputStream()) from a different connection object than what it should have been. I fixed the connection object variable and it started working.

    BufferedImage image;
     try (InputStream in = new BufferedInputStream(conn.getInputStream())) {
       image = ImageIO.read(in);
       File file = new File(fullImageFilePath);
       synchronized (file.getCanonicalPath().intern()) {
         if (!file.exists()) {
             ImageIO.write(image, "png", file);
         }
       }
     }
    
    0 讨论(0)
  • 2020-11-27 23:08

    Try this:

    this.icon = ImageIO.read(this.getClass().getResource("/resources/" + imgName));
    
    0 讨论(0)
  • 2020-11-27 23:10

    Try using the following

    this.icon = ImageIO.read(this.getClass().getResourceAsStream("../resources/" + imgName));
    
    0 讨论(0)
提交回复
热议问题