Im trying to load an image from my project directory into a BufferedImage resource, but getting an error stating that it cannot load the image. Here is the line providing the error:
final BufferedImage plusMinusIcon = ImageUtil.loadImageResource(CalculatorProPlugin.class, "plus_minus_icon.png");
And here is the error I receive when trying to build:
net.runelite.client.util.ImageUtil - Failed to load image from class:
...plugins.calculatorpro.CalculatorProPlugin path:
...plugins.calculatorpro/plus_minus_icon.png
The image is saved in the projects directory, and if I copy the path to "plus_minus_icon.png" from the directory, I get "...plugins.calculatorpro\plus_minus_icon.png", so it matches what I put in the code
WORKING ANSWER: Using Frakcool's suggestions from below, he helped me to create a working solution:
InputStream inputStream = CalculatorProPlugin.class.getResourceAsStream("/plus_minus_icon.png");
BufferedImage plusMinusIcon = null;
plusMinusIcon = ImageIO.read(inputStream);
With my icons stored in a resource folder within the project directory:
Use an embedded-resource
instead:
InputStream inputStream = this.getClass().getResourceAsStream("plus_minus_icon.png");
Then convert the input stream as shown in this answer:
BufferedImage plusMinusIcon = ImageIO.read(inputStream);
I entered your code, but after running, "inputStream == null", returns true
Your image should be located in a resources
folder, as shown in the How to use icons tutorial, also see this answer for more information, and you can also refer to this tutorial for the explanation on why it needs to be in a resources
folder.
When I System.out.println(inputStream)
this is what I get:
java.io.BufferedInputStream@57829d67
If it's not in the resources folder, I get the same error as you
Runnable example:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
public class ImageLoader {
public static void main(String[] args) {
ImageLoader imageLoader = new ImageLoader();
imageLoader.loadResource();
}
private void loadResource() {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("plus_minus_icon.png");
System.out.println(inputStream);
try {
BufferedImage plusMinusIcon = ImageIO.read(inputStream);
System.out.println(plusMinusIcon);
} catch (IOException e) {
e.printStackTrace();
}
}
}