Java: How to get the thumbnail from a file

Martijn Courteaux picture Martijn Courteaux · Sep 30, 2009 · Viewed 13.2k times · Source

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.

Something like:

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

Thanks

Answer

Powerlord picture Powerlord · Sep 30, 2009

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.