2

Hi I am trying to get path of BufferedImage but how to get path of that loaded image i don't know. I am fetching image from Stack<>. one by one image fetched from it when user click on next button. image changed using pop() method of stack.

code :

        Stack<File> pictures ;
        final JFileChooser file;
        file = new JFileChooser();
        file.setCurrentDirectory(dir);
        file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        file.showOpenDialog(panel);
        String path = file.getSelectedFile().getAbsolutePath();
        System.out.println(path);
        pictures= getFilesInFolder(path.toString());    


        a=ImageIO.read(pictures.pop().getAbsoluteFile());

here a is Buffered Image instance. now i want whole path of image that loaded in a.

anyone guide me ?

4

3 回答 3

2

问题是您无法从 BufferedImage 中读取路径,因此您必须在制作 BufferedImage 之前使用该文件。

所以你可以使用:

String path = stack.peek().getPath();

现在你已经保存了你的路径。目前,您将其转换为 BufferedImage 使用pop(),以便将其从堆栈中删除。与peek()你只看第一个项目而不删除。或者您将文件保存到临时文件中,例如

File temp = stack.pop();

比你能够使用:

temp.getPath();
于 2013-08-22T09:21:07.673 回答
2

ABufferedImage不维护任何关于它如何生成或从何处加载的信息。在加载图像之前,您必须将文件路径存储在变量中:

    File file = pictures.pop().getAbsoluteFile();
    a=ImageIO.read(file);
    // now you can use "file" for other purposes too
于 2013-08-22T09:12:18.277 回答
0

是的,我已经使用以下代码解决了这个问题:

            String p;
            File f;
            try 
            {
                f= pictures.pop().getAbsoluteFile();
                a=ImageIO.read(f);
                p = f.getPath();
                System.out.println(p);


            } 
            catch (IOException e1) 
            {
                e1.printStackTrace();
            }
于 2013-08-22T13:49:26.430 回答