1

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

enter image description here

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: enter image description here

1 Answers1

0

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.

enter image description here

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();
        }
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
  • I entered your code, but after running, "inputStream == null", returns true – TravelKidKurty Apr 20 '23 at 18:05
  • @TravelKidKurty please see the edited answer, and please there's no need to remove and post your comment again, I know you're desperate for an answer but we're volunteers here, and I'm squeezing some time from my job to provide some help here, reposting your comment to drag the attention of someone can be annoying to some, so please, avoid doing so. – Frakcool Apr 20 '23 at 18:09
  • 1
    Apologies, I did not intend to double-notify you. Im fairly new here, so I thought my initial comment would not notify you without having your nametag referenced at the beginning, so I deleted the comment and included your tag, which was automatically removed when I re-posted. I appreciate your info, Ill have a look through all you sent and update back here after! – TravelKidKurty Apr 20 '23 at 19:19
  • 1
    @TravelKidKurty If you're using IntelliJ then [this](https://www.youtube.com/watch?v=ABXiJkCcevc) might be handy for creating the resource folder – Frakcool Apr 20 '23 at 20:10
  • And btw, if you're replying to the answerer on their answer they get notified immediately, if you're replying to someone else then there's when you want to tag them. Same thing happens when you comment under the question :) – Frakcool Apr 20 '23 at 20:11
  • 1
    Good to know, haha thanks! Also thanks for that intelliJ resource folder link- that was very helpful! I got pulled away from the project yesterday, so I havent finished implementing everything youve sent just yet, but will update you as I go (might be today, or not 'till next week) – TravelKidKurty Apr 21 '23 at 19:00
  • I still receive "null" when I print inputStream... I notice in all the examples youve sent that I read, everyone uses this.getClass() or some variety thereof. Since my code is in a static context, IntelliJ does not let me use it. Instead I use CalculatorProPlugin.class. Would this be doing something different than the getClass() function? Other than that, I don't know what Im possibly doing different from any of the examples Ive looked at and tried – TravelKidKurty Apr 21 '23 at 19:59
  • (also all my .png files are now in a resources folder within the calculatorpro folder)- I could update the OP with new screenshots of code and file structure if that would help? – TravelKidKurty Apr 21 '23 at 20:06
  • @TravelKidKurty it would help but no screenshots of code, but the code as code-formatted text instead. I'm curious about what you're saying of *in a static context* you should avoid that as much as possible. I've included an example, which creates an instance (and that allows you to use `this`, and it's no longer in a static context since now you have an instance) – Frakcool Apr 21 '23 at 20:38
  • I finally got it working just before you posted your last edit! I added the getClassLoader() function, and boom- worked like a charm! Upon further reading of your link labelled as "this answer" (Im unsure how to post a hyperlink in the comments), I found I can also get it to work without the getClassLoader() function if I simply add a '/' to the filename string ("/plus_minus_icon.png")- I swear I already tried this in an earlier attempt, but perhaps I had some other part of the function incorrect – TravelKidKurty Apr 21 '23 at 20:58
  • @TravelKidKurty you can post it by `[The text you want to post](The url it should point to)` – Frakcool Apr 21 '23 at 21:01
  • [This](https://stackoverflow.com/questions/9864267/loading-resources-like-images-while-running-project-distributed-as-jar-archive/9866659#9866659) is the link I was referring to that you posted above. I updated the OP to show my final working code and a screenshot of the directory that you helped me to create. Thanks a lot!! – TravelKidKurty Apr 21 '23 at 21:06