ImageIO can't read input file

后端 未结 1 1497
灰色年华
灰色年华 2020-12-20 12:55
public static void imRes(String pat) {
        try {
            BufferedImage bckimg = ImageIO.read(new File(\"c:/s/deneme.jpg\"));
            File s = new File(pa         


        
相关标签:
1条回答
  • 2020-12-20 13:31

    Change / for \ if you are using windows.

    A more cross-platform approach would be substitute

    C: for File.listRoots()[0] and every / for File.separator.

    Read more on the File api documentation

    EDIT

    (I didn't read this line, sorry)

    This code was running properly untill i try to read an image from the source package

    In order to get a file from inside your jar package, one must use the getClass().getResource() method.

    Example:

    application-package:
    |-Main.java
    |-resources
      |-image.jpg
    

    For the above directory structure:

    BufferedImage im = ImageIO.read(new File(getClass().getResource("/resources/image.jpg").toURI()));
    

    Would do the trick.

    0 讨论(0)
提交回复
热议问题