ImageIO.read returns NULL, with no errors

后端 未结 3 700
无人及你
无人及你 2020-11-28 13:45

The following code seems not to work, even though the file appears to be found just fine.

    images = new BufferedImage[32];
    FileInputStream fis = null;         


        
相关标签:
3条回答
  • 2020-11-28 14:10

    Try wrap you InputStream into BufferedInputStream:

    fis = new FileInputStream(file); ==> new BufferedInputStream(new FileInputStream(file));

    0 讨论(0)
  • ImageIO.read(file); will return null if no registered ImageReader is found. Please check whether you have registered any ImageReader.

    I think this code snippet could help you

    File file = new File("bear.jpg"); // I have bear.jpg in my working directory  
        FileInputStream fis = new FileInputStream(file);  
        BufferedImage image = ImageIO.read(fis); //reading the image file  
    

    You just need to wrap the file into an FileInputStream and then pass it to read()

    0 讨论(0)
  • 2020-11-28 14:20

    ImageIO.read(*...) will only load these image types GIF, PNG, JPEG, BMP, and WBMP.

    Any other image type will return null without error.

    reference: http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html

    I do realize this is not a solution to the specific original problem but it is a solution to the question asked.

    0 讨论(0)
提交回复
热议问题