2

I was looking at this question and I was looking at the first answer.

So I tried to use this code:

public static Image getIcon(String fileName) throws Exception {
    File file = new File(fileName);
    FileSystemView view = FileSystemView.getFileSystemView();
    Icon icon = view.getSystemIcon(file);
    ImageIcon imageIcon = (ImageIcon) icon;
    Image image = imageIcon.getImage();
    return image;
}

Which does return an Image (or throws an Error) but the Image has terribly low resolution.

I am assuming that this is because the 16x16 Image is returned.

Is there any way to state which Image I want to be returned?

Community
  • 1
  • 1
nick zoum
  • 7,216
  • 7
  • 36
  • 80

2 Answers2

2

Java offers you two possibilities to retrieve file icons.
You already know the first one:

Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File(FILENAME));

that gives you a 16x16 pixel result.

The other one using ShellFolder

Icon icon = new ImageIcon(ShellFolder.getShellFolder(new File(FILENAME)).getIcon(true));

will retrieve you the larger one (32x32) depending on the boolean flag getLargeIcon in the getIcon method.
I'm sorry for you but more is (at the moment) not possible with the java default libraries. Interest exists as you can read in this JDK bugreport. But nothing has been done so far.

If you really want to have larger versions you will need to retrieve them with the OS depending native calls or store them manually as local application ressources.

Note: If you have problems accessing ShellFolder you should read this question.

Community
  • 1
  • 1
ArcticLord
  • 3,999
  • 3
  • 27
  • 47
  • So the method `getIcon` will return the larger image if the parameter is true and the smaller image if the parameter is false? So this: `return ShellFolder.getShellFolder(new File(fileName)).getIcon(true);` Will return the image with best resolution? – nick zoum Oct 14 '16 at 10:07
  • Exactly. And that is the best you can get. – ArcticLord Oct 14 '16 at 10:08
  • Well the resolution has definitely improved since before. So thanks. – nick zoum Oct 14 '16 at 10:09
0

I used this method:

protected ImageIcon getImageIcon() {
    File f = new File((iconPath!=null)?iconPath:"");
    if (!f.isFile() || !f.canRead()) {
        iconPath = Constants.getDefaultPreviewIconPath();
    }
    ImageIcon icon = new ImageIcon(iconPath, titolo);
    return new ImageIcon(Utils.getScaledImage(
            icon.getImage(), 
            Constants.getICON_WIDTH(), 
            Constants.getICON_HEIGTH()));
}

where getScaledImage is:

public static Image getScaledImage(Image srcImg, int w, int h) {
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return resizedImg;
    }
aurox
  • 107
  • 1
  • 3
  • 8