问题
Okay so I'm trying to make an API for my intro screen to be used across multiple games and I've spent the last 5-6 hours trying to figure out this 1 problem.
Lets say that I have the API.jar referenced in on of my games and I'm trying to call the method getByteArray() which is supposed to load the frame as a BufferedImage then return the byte[] data. I constantly get input == null when attempting to load the image.
public byte[] getByteArray() throws IOException {
// Open Image
String url = "/com/jumpbuttonstudio/api/resources/intro/Frame (" + current_frame + ").jpg";
System.out.println("Loading: " + url);
BufferedImage img = ImageIO.read(getClass().getClassLoader().getResourceAsStream(url));
DataBufferByte data = (DataBufferByte) img.getRaster().getDataBuffer();
return (data.getData());
}
I really need help with this, I can provide more information if required.
NOTE
The code above is in the API.jar and so are all the images.
回答1:
If you use Class.getResourceAsStream(path)
, the path must start with a /
(otherwise it's considered as a path relative to the class package). If you use ClassLoader.getResourceAsStream(path)
, the path must NOT start with a /
.
来源:https://stackoverflow.com/questions/20761413/imageio-inside-jar-file