Image to byte[]

Hey there,
I’m a bit lost on how to change an Image (from a ThemeRessource) to a byte. Can someone please help me?

This is the code for the image:
Image image = new Image(null, new ThemeResource(Config.getString("SMALL_LOGO_IMAGE", "")));

Another approach I tried was (placing the Image somewhere else):

ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            BufferedImage image = ImageIO.read(getClass().getResource("/resources/" + Config.getString("SMALL_LOGO_IMAGE", "")));
            ImageIO.write(image, "png", bos);
           
            
         } catch (IOException e) {
             e.printStackTrace();
         }
return bos.toByteArray();

"/resources/" + Config.getString("SMALL_LOGO_IMAGE", "") returns the correct path and image. But I’m getting the message "Input == null".
When using getResource("/resources/" + Config.getString("SMALL_LOGO_IMAGE", "")).toString() I’m getting a NullPointerException

Thanks a lot in advance :slight_smile:

How do you call the getResource method? Different classloaders might have different base paths, leading to not being in the correct directory. Speaking of, where is your ‘resources’ directory?

Thomas Mattsson:
How do you call the getResource method? Different classloaders might have different base paths, leading to not being in the correct directory. Speaking of, where is your ‘resources’ directory?

Hi, yeah. Thanks, this might be the problem. I also tried with Files, which tells me that the Image can’t be found. But I didn’t work until today, so I didn’t have a chance to find a solution :slight_smile:

It’s like this (Both are in the same Project):

Project/src/main/resources/xy.png
Project/src/main/java/manyOtherDirs/classThatCallsTheMethod

But then I’m building a war-file and in my tomcat dir it is like this:
Project/WEB-INF/classes/xy.png

Not sure if this makes a difference in regards to the path?

I tested this snippet and it works for me;

getClass().getClassLoader().getResource("README.MD")

Where the readme file is just under main/src/resources.