ImageIO.read() infinite loop

2019-08-07 19:30发布

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条回答
\"骚年 ilove
2楼-- · 2019-08-07 20:06

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.

查看更多
登录 后发表回答