18

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

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287

4 Answers4

18

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.

Community
  • 1
  • 1
Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • 1
    This is what I want. Thanks, maybe you know also a way for bigger icons? – Martijn Courteaux Sep 30 '09 at 15:52
  • 1
    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 Oct 08 '09 at 12:19
  • Is this code platform independent? It did not work for me (Linux Mint 18.3) – Vijay Chavda Dec 17 '18 at 14:10
  • this works. you can recalculate the images with bufferedImage, and if you use a transparent panel, you have really nice icons , like on the desktop. works for binaries and folders. I think a "normal" image dropped onto the application is better loaded the classic way, tho ^^ – clockw0rk Sep 02 '20 at 01:04
6

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Andy Ross
  • 11,699
  • 1
  • 34
  • 31
3

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.

giraff
  • 4,601
  • 2
  • 23
  • 35
0

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_assoc/

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

pjp
  • 17,039
  • 6
  • 33
  • 58
  • The java.sun.com link can be viewed [at archive.org](https://web.archive.org/web/20061030164547/http://java.sun.com/developer/technicalArticles/J2SE/Desktop/jdic_assoc/), and there is a semi-readable version [at oracle.com](http://www.oracle.com/technetwork/articles/java/jdic-assoc-136531.html). The forum link is not archived, unfortunately. – VGR Oct 07 '16 at 14:01