Java:如何从文件中获取缩略图 [英] Java: How to get the thumbnail from a file
问题描述
在Windows资源管理器(和linux gnome)中为每个文件显示缩略图或图标。与EXE,图像,视频,txt文件,ini文件,Word文档和其他许多文件类型一样。现在是我的问题:有没有办法按文件获得(最好是大)图标。
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 much other file types. Now is my question: Is there a way to get a (preferably large) icon by file.
类似于:
public static BufferedImage getThumbnail(File file) {...}
谢谢
推荐答案
看起来没有AWT方法可以做到这一点。
It looks like there is no AWT way of doing this.
如果您使用Swing,可以使用一种方法来获取Swing 图标
...
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);
您还可以将图标
转换回一个图像
(很可能是BufferedImage,但我还没有测试过):
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();
还有一个类直接获取图标,但此类依赖于JDK。在Sun JDK上,它是 sun.awt.shell.ShellFolder
类。
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.
如果您正在使用SWT,那么事情有点棘手。
If you're using SWT, things are a bit trickier.
这篇关于Java:如何从文件中获取缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!