Java – InputStream closed by imageio

I encountered a very strange problem with imageio

Some objects in my java program have an image I use the static method to load these images through my imagemanager

public static Image load(String path){
    Image img = null;
    try {
        img = ImageIO.read(ImageManager.class.getResource(path));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    if(img == null){
        System.out.println("Image: '" + path + "' not found!");
    }

    return img;
}

Whenever I load a (static) background image or load an image in my rendering method, there is no problem

In addition, there is no problem loading images into static variables in my card class However, whenever I dynamically load an image from my card constructor, I throw an exception saying that the stream is closed

java.io.IOException: closed
Image: '/textures/cards/back.png' not found!
at javax.imageio.stream.ImageInputStreamImpl.checkClosed(ImageInputStreamImpl.java:110)
at javax.imageio.stream.ImageInputStreamImpl.close(ImageInputStreamImpl.java:857)
at javax.imageio.stream.FileCacheImageInputStream.close(FileCacheImageInputStream.java:250)
at javax.imageio.ImageIO.read(ImageIO.java:1451)
at javax.imageio.ImageIO.read(ImageIO.java:1400)
at visual.gfx.ImageManager.load(ImageManager.java:15)
at visual.Card.<init>(Card.java:49)
at visual.WorldController.render(WorldController.java:98)
at visual.WorldController.run(WorldController.java:116)
at java.lang.Thread.run(Thread.java:745)

So be clear: this:

public class Card{
    public static Image BACK = ImageManager.load("/textures/cards/back.png");
}

He works very well

And this:

public class Card{
    private Image image;

    public Card(){
        image = ImageManager.load("/textures/cards/back.png");
    }
}

The result is IOException

Can someone tell me why and how I can do things in different ways?

Edit:

I'm not sure how or why this exception occurred, but I'll try to fix it temporarily

My program structure is as follows:

public void run(){
    init();
        while(running){
            update();
            render();
        }
    finish();
}

I plan to load all images and create all instances from the init method But to test my loading method, I quickly created a card instance

But when I try to load the image, it throws an exception Loading images from the init () method solves my problem

Anyone can tell me why this makes such a difference

By the way, this is my rendering method

public void render(){
    BufferStrategy bs = getBufferStrategy();
    if(bs == null){
        createBufferStrategy(2);
        bs = getBufferStrategy();
    }

    Graphics g = bs.getDrawGraphics();
    g.drawImage(background,null);

    // render actual game

    game.render(g);

    // ------------------

    g.dispose();
    bs.show();
}

>I did add a res folder to the project to find the images: ~ / RES / textures / cards / back png

Solution

ImageIO. The Javadoc of read (URL) is a bit vague, but it seems to cache the input stream based on URL internally, and its behavior is not obvious in multi-threaded environment

If I were you, I would give imageio Read (InputStream) a shot

I hope this will help

to greet,

Slava Imeshev

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>