Question

Well I'm realy at a loss here. I try some JOGL and want to get a texture on an Object. I usually do it like this:

Texture[] thumbs = new Texture[pics.length];

try {
    for (int i = 0; i < thumbs.length; i ++){
        InputStream stream = getClass().getResourceAsStream(pics[i].getPath());
        data = TextureIO.newTextureData(stream, false, "jpg");
        thumbs[i] = TextureIO.newTexture(data);
    }
} catch (IOException e) {
    e.printStackTrace();
}

Usually this works fine if the jpg-file is in the source-directory but this time the file lies elsewhere and I recieve an IOException that says the stream was null.

pics[i].getPath() returns this String: C:\beispieluser\bjoern\eigene_bilder\eckelsheim.jpg. This is the exact path where the file lies. Can somebody tell me where my thoughts took the wrong turn?

Was it helpful?

Solution

getResourceAsStream() and friends will only open "classpath resources", which are files that appear on the classpath along with your compiled classes. To open that file, use new File() or (on Java 7) Files.newInputStream().

OTHER TIPS

getResourceAsStream() finds resources that are in the classpath. I'm pretty sure it won't work for an absolute path somewhere else on your disk.

  1. You files folder is not a part of your classpath. You can add it, but i don't think it proper way for resources like images to be a part of classpath.
  2. Use FileInputStream:

Code:

try{
    for (int i = 0; i < thumbs.length; i ++){
      File file = new File(pics[i].getPath());
      FileInputStream stream = new FileInputStream(file);
      data = TextureIO.newTextureData(stream, false, "jpg");
      thumbs[i] = TextureIO.newTexture(data);
    }
} catch (IOException e) {
   e.printStackTrace();
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top