문제

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?

도움이 되었습니까?

해결책

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().

다른 팁

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();
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top