我正在看这个question我正在查看第一个答案。
所以我尝试使用这段代码:
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;
}
它确实返回Image
(或抛出Error
),但Image
的分辨率非常低。
我假设这是因为返回了16x16 Image
。
有什么方法可以说明我想要返回哪个图像
?
最佳答案
Java 为您提供了两种检索文件图标的可能性。
您已经知道第一个:
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File(FILENAME));
这将为您提供 16x16 像素的结果。
另一种使用 ShellFolder
Icon icon = new ImageIcon(ShellFolder.getShellFolder(new File(FILENAME)).getIcon(true));
将根据 getIcon
方法中的 boolean 标志 getLargeIcon
检索较大的图标 (32x32)。
我为你感到抱歉,但是(目前)使用 java 默认库无法实现更多功能。兴趣存在,您可以阅读 in this JDK bugreport 。
但到目前为止还没有采取任何行动。
如果您确实想要拥有更大的版本,则需要使用操作系统(取决于 native 调用)检索它们,或者手动将它们存储为本地应用程序资源。
注意:如果您在访问 ShellFolder
时遇到问题,您应该阅读 this question .
关于java - Java中获取文件特定版本的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39918487/