簡體   English   中英

getResourceAsStream 在圖像路徑上返回 null

[英]getResourceAsStream returning null on image path

我已經閱讀了許多關於這個問題的其他問題和資源,但仍然一無所獲。 前幾天我問了一個關於這個的問題,然后又被提到了其他問題。 也許我只是沒有得到它,但我一直在嘗試為我的 java 項目 getResourceAsStream 2 天,所以我可以讓 jar 工作無濟於事。

我的項目中有兩個地方導致問題。 我的緩沖圖像類:

public static BufferedImage loadImage(String fileName) {
        try { 
            File file = new File(fileName);           
            //System.out.println("Is the file there for " + fileName + " : "  + file.exists());
            BufferedImage sub = ImageIO.read(file); 
            return toCompatibleImage(sub);
        }
        catch (IOException e) { 
            System.out.println("Image load failed: " +fileName);
//          e.printStackTrace();
            return null;
        }
    }

我的 GifHandler 類:

public enum GifHandler {
    Mouse("Resources/Images/cursor/", ".gif", 45, 45),
    Coaster("Resources/Images/animatedCoaster/", ".gif", Carnies.PWIDTH, Carnies.PHEIGHT/2),
    Fire("Resources/Images/fire/", ".gif", 100, 100);

    public BufferedImage[] sequence;
    int playing = 0;

    GifHandler(String dir_name, String type, int width, int height) {
        int numImgs = new File(dir_name).list().length;
        if (sequence == null) {
            sequence = new BufferedImage[numImgs];
            for (int i = 0; i < numImgs; i++) {
                sequence[i] = ImageLoader.loadScaledImage(dir_name + i + type, width, height, false);
            }
        }
    }
}

我已將資源文件夾添加到我的構建路徑庫中。 我的項目文件結構是這樣的: "Project/src/defaultpackage/classfiles" "Project/resources/Images/imagefiles" 我想從類文件中引用圖像文件,顯然。 我曾嘗試更改為 getResourceAsStream,但始終在路徑上獲取空值。

例如,對 imageLoader 類的更改如圖所示,返回 null:

字符串路徑 = ImageLoader.class.getResourceAsStream(fileName).toString(); 例如,fileName 作為“resource/Images/alphabet.png”傳入。 我已經嘗試了添加和刪除 / 的所有組合,只引用 /alphabet.png 而沒有前導文件夾等。我已經走到了盡頭。 請幫忙。 如果我沒有鏈接足夠的代碼以使其有意義,它的回購位於 github.com/madamsmall/carnies

我的項目文件結構是這樣的:“Project/src/defaultpackage/classfiles”“Project/resources/Images/imagefiles”

當您使用getClass().getResourceAsStream() ,程序將從調用類的位置開始搜索。 因此,通過傳遞"resource/Images/alphabet.png" ,您是說resourcesclassfiles ,而事實並非如此。 要修復它,您需要允許回溯搜索。 您可以通過在路徑中添加/來實現。 所以getClass().getResourceAsStream("/resource/Images/alphabet.png")

另一種選擇是使用類加載器,它將從根搜索。 在這種情況下,您不需要額外的/並且您當前的路徑是正確的。 getClass().getClassLoader().getResourceAsStream("resource/Images/alphabet.png");

注意: getClass()ImageLoader.class在大多數情況下是可以互換的,以防你想知道我為什么使用getClass()


更新

使用此示例作為測試

import java.awt.Image;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

public class ImageLoader {

    public static void main(String[] args) throws IOException {
        InputStream is = ImageLoader.class.getResourceAsStream("/resource/images/alphabet.png");
        Image image = ImageIO.read(is);
        JLabel label = new JLabel(new ImageIcon(image));
        JOptionPane.showMessageDialog(null, label);

    }
}

更新 2

我只是注意到resource不在你應該在的src中,所以當它被構建時,它被構建到類路徑中。


更新 3

如果要獲取所有文件名的列表,可以使用此方法。 然后你可以對這些文件名做任何你想做的事情。

private String[] getFileNames() throws URISyntaxException {
    URL url = getClass().getResource("/resources/images/");
    File file = new File(url.toURI());
    String[] fileNames = file.list();
    for (String s : fileNames) {
        System.out.println(s);
    }
    return fileNames;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM