Java: How to get the thumbnail from a file

前端 未结 4 783
孤城傲影
孤城傲影 2020-12-06 01:15

In windows explorer (and linux gnome) shows for each file a thumbnail or icon. Like with EXEs, images, videos, txt-files, ini-files, Word documents and so m

4条回答
  •  自闭症患者
    2020-12-06 01:21

    It looks like there is no AWT way of doing this.

    If you're using Swing, there is a method you can use to get a Swing Icon...

    import javax.swing.filechooser.FileSystemView;
    import javax.swing.Icon;
    

    ...

    Icon ico = FileSystemView.getFileSystemView().getSystemIcon(file);
    

    You can also convert that Icon back into an Image (most likely a BufferedImage, but I haven't tested it):

    Image image = ((ImageIcon) ico).getImage();
    

    There is also a class to get the icon directly, but this class is JDK dependent. On Sun JDK's it's the sun.awt.shell.ShellFolder class.

    If you're using SWT, things are a bit trickier.

提交回复
热议问题