简体   繁体   English

ImageIO.read 返回 NULL,没有错误

[英]ImageIO.read returns NULL, with no errors

The following code seems not to work, even though the file appears to be found just fine.以下代码似乎不起作用,即使该文件似乎找到得很好。

    images = new BufferedImage[32];
    FileInputStream fis = null;
    for (int i = 0; i < 32; i++) {
        File file = new File("tiles\\"+i+".bmp");
        if (!file.exists()){
            System.out.println("File  "+i+" failed");
        }
        try { 
            fis = new FileInputStream(file); 
        } catch (FileNotFoundException e) { 
            System.err.println(e + "" + i); 
        }
        try { 
            images[i] = ImageIO.read(fis); 
        } catch (IOException e) { 
            System.err.println(e + "" + i); 
        }
        if (images[i] == null) {
            System.out.println("Image "+i+" failed");
        }
    }

Thanks in advance for any help.提前感谢您的帮助。

Edit: The result is me attempting to Graphics.drawImage(images[0]);, and it giving me a null pointer exception.编辑:结果是我尝试 Graphics.drawImage(images[0]);,它给了我一个 null 指针异常。 This code here completes fine.这里的代码完成得很好。

Edit: Changed moved the if(.file,exists()) as suggested.编辑:按照建议更改了 if(.file,exists()) 的移动。 and wrapped the file in an input stream.并将文件包装在输入流中。

ImageIO.read(*...) will only load these image types GIF , PNG , JPEG , BMP , and WBMP . ImageIO.read(*...)将只加载这些图像类型GIFPNGJPEGBMPWBMP

Any other image type will return null without error.任何其他图像类型都将无错误地返回null

reference:http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html参考: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.我确实意识到这不是特定原始问题的解决方案,而是所提出问题的解决方案。

ImageIO.read(file); ImageIO.read(文件); will return null if no registered ImageReader is found.如果未找到已注册的ImageReader,则返回 null。 Please check whether you have registered any ImageReader .请检查您是否已注册任何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()您只需要将文件包装到FileInputStream 中,然后将其传递给read()

Try wrap you InputStream into BufferedInputStream:尝试将您的 InputStream 包装到 BufferedInputStream 中:

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

使用new File(getClass().getResource("tiles\\\\"+i+".bmp"));

Add the following dependencies:添加以下依赖项:

<dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-core</artifactId>
        <version>1.4.0</version>
    </dependency>
    <dependency>
        <groupId>com.github.jai-imageio</groupId>
        <artifactId>jai-imageio-jpeg2000</artifactId>
        <version>1.4.0</version>
    </dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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