ImageIO.read() infinite loop

青春壹個敷衍的年華 提交于 2019-12-24 13:01:07

问题


ImageIO.read() just seems to be stuck in an infinite loop.

My code:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Texture {
    BufferedImage _img;

    public Texture(String path) {
        try {
            _img = ImageIO.read(new File(path));
        } catch (final IOException e) {
            e.printStackTrace();
        }
    }
}

Other class:

private Texture _tex;
_tex = new Texture("/res/img.png");

I have tried loading the image this URL and File, none works.

I am using eclipse, on a mac, and I am using LWJGL 3 if that has anything to do with anything.

Hope you can help me! :-)


回答1:


Instead of using a File, try using an input stream like this:

InputStream stream = Texture.class.getResourceAsStream(path);
_img = ImageIO.read(stream);

I'm actually experiencing a similar issue, but it's related to the creation of BufferedImages.



来源:https://stackoverflow.com/questions/32635764/imageio-read-infinite-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!