views:

506

answers:

3

Hello,

In windows explorer (and linux gnome) views for each file a thumbnail / icon. Like with EXEs, images, videos, txt-files, ini-files and so much other file types. Now is my question: Is there a way to get them by file.

Something like:

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

Thanks

A: 

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

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic%5Fassoc/

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

pjp
+2  A: 

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.

R. Bemrose
This is what I want. Thanks, maybe you know also a way for bigger icons?
Martijn Courteaux
Yes, is there a way to get bigger icons?I would like to use sun.awt.shell.ShellFolder.getIcon(true), but sun.awt.shell.ShellFolder has access restriction. :-(
Lars A Frøyland
+1  A: 

I don't think the answers above are addressing the right question. Martjin 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.

Andy Ross