Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to get the thumbnail from a file

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

like image 427
Martijn Courteaux Avatar asked Sep 30 '09 14:09

Martijn Courteaux


3 Answers

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.

like image 101
Powerlord Avatar answered Nov 17 '22 13:11

Powerlord


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.

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/

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.

like image 39
Andy Ross Avatar answered Nov 17 '22 13:11

Andy Ross


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.

like image 3
giraff Avatar answered Nov 17 '22 15:11

giraff