java 获取文件版本,使用Java获取文件图标的特定版本

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?

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值