ImageIO.read() infinite loop

2019-08-07 19:40发布

问题:

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.