Jump to content

Hi,

I'm building a project in java and I need to import an image file inside the res folder, but I've tried to add it to the build path but i still can't access. Although it looks like it's added as you can see from the pictures.

 

This is where the code throws an exception:

 

class someclass{

private static BufferedImage image = ImageReader.readToBufferedImage("/textures/letters.png");
/*

....

*/

}

 

public class ImageReader{

    public static BufferedImage readToBufferedImage(String path){
        BufferedImage image;
        try{
            image = ImageIO.read(new File(path));
            return image;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

The exception that gets thrown by the image reader is:

 

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(Unknown Source)

 

Have I missed something. Thanks in advance.

Capture.PNG

Capture2.PNG

Capture.PNG

Capture2.PNG

!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!CATS!!

Link to post
Share on other sites

  • 2 weeks later...

You sure the path is correct, it may not be ideal to start from root.

 

Try simply removing the front slash.

 

File f = new File("textures/letters.png");
if(f.exists() && !f.isDirectory()) { 
    BufferedImage image = ImageReader.readToBufferedImage("textures/letters.png");
}

Try that, not tested though.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×