Java:如何从文件中获取缩略图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1498506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:46:50  来源:igfitidea点击:

Java: How to get the thumbnail from a file

javafileswingthumbnails

提问by Martijn Courteaux

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.

在 Windows 资源管理器(和 linux gnome)中,为每个文件显示一个缩略图或图标。与 EXE、图像、视频、txt 文件、ini 文件、Word 文档和许多其他文件类型一样。现在是我的问题:有没有办法按文件获取(最好是大的)图标。

Something like:

就像是:

public static BufferedImage getThumbnail(File file) {...}

Thanks

谢谢

采纳答案by Powerlord

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

看起来没有 AWT 方法可以做到这一点。

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

如果您使用的是 Swing,则可以使用一种方法来获取 Swing Icon...

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

...

...

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

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

您还可以将其转换IconImage(很可能是 BufferedImage,但我还没有测试过):

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.ShellFolderclass.

还有一个类可以直接获取图标,但是这个类是JDK依赖的。在 Sun JDK 上,它是sun.awt.shell.ShellFolder类。

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

如果您使用 SWT,事情就有点棘手了

回答by Andy Ross

I don't think the answers above are addressing the right question. Martijn isn't asking for the API for manipulating icon images. He wants to know how to get to the desktop's cache of thumbnail images for items in the browsable filesystem.

我认为上面的答案没有解决正确的问题。Martijn 并不需要用于操作图标图像的 API。他想知道如何访问可浏览文件系统中项目的缩略图图像的桌面缓存。

I don't think there's a portable way to do this at all, but the Linux world (Gnome and KDE) adhere to the Freedesktop thumbnail standard which can be read at: http://jens.triq.net/thumbnail-spec/

我认为根本没有一种可移植的方式来做到这一点,但 Linux 世界(Gnome 和 KDE)遵循 Freedesktop 缩略图标准,可以在以下位置阅读:http: //jens.triq.net/thumbnail-spec/

There may be code, perhaps in Gtk (but probably not Java) that implements a compatibility layer for windows, but I'm not aware of it if there is.

可能有代码,也许在 Gtk(但可能不是 Java)中实现了 Windows 的兼容层,但我不知道它是否存在。

回答by giraff

If you are looking for a plattform-independent (i.e. pure java) solution, hava a look at JavaThumbnailer. It's still beta, though, and has a limited number of supported file types.

如果您正在寻找独立于平台(即纯 Java)的解决方案,请查看JavaThumbnailer。不过,它仍然是测试版,支持的文件类型数量有限。

回答by pjp

Not entirely sure if you will be able to get the icon but take a look at AssociationServicethat is accessible using JDIC.

不完全确定您是否能够获得该图标,但请查看AssociationService使用 JDIC 可以访问的图标。

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/

Also this thread http://forums.sun.com/thread.jspa?threadID=5357995

还有这个线程http://forums.sun.com/thread.jspa?threadID=5357995