获取Java中文件图标的特定版本
我在看这个,我在看第一个答案 因此,我尝试使用以下代码:获取Java中文件图标的特定版本,java,file,filesystems,icons,thumbnails,Java,File,Filesystems,Icons,Thumbnails,我在看这个,我在看第一个答案 因此,我尝试使用以下代码: public static Image getIcon(String fileName) throws Exception { File file = new File(fileName); FileSystemView view = FileSystemView.getFileSystemView(); Icon icon = view.getSystemIcon(file); ImageIcon ima
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;
}
返回图像
(或抛出错误
),但图像
的分辨率极低
我假设这是因为返回了16x16图像
有没有办法说明我想返回哪个Image
图像?我使用了以下方法:
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()));
}
其中GetScaleImage是:
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;
}
Java为您提供了两种检索文件图标的方法。
您已经知道第一个:
Icon icon = FileSystemView.getFileSystemView().getSystemIcon(new File(FILENAME));
这将为您提供16x16像素的结果。另一个使用
ShellFolder
Icon icon = new ImageIcon(ShellFolder.getShellFolder(new File(FILENAME)).getIcon(true));
将根据getIcon
方法中的布尔标志getLargeIcon
检索较大的一个(32x32)。我为您感到抱歉,但java默认库(目前)无法提供更多功能。兴趣是存在的,正如你所能读到的。 但到目前为止还没有采取任何行动。
如果您真的想拥有更大的版本,则需要使用依赖于操作系统的本机调用检索它们,或者将它们手动存储为本地应用程序资源。
注意:如果您在访问
ShellFolder
时遇到问题,您应该阅读。放大16×16图像与检索系统更高分辨率版本的图像不同。“titolo”是一个字符串,用作我类的一个字段,与“iconPath”相同@VGR好的,我误解了问题“特定版本”和“哪个图像,”你是指同一类型的系统提供的图像,但具有更高的分辨率,如48×48,对吗?@VGR这是正确的也许这个链接可以帮助你@MikhailKuchma Java找不到方法,我得到NoSuchMethodException
,因此如果参数为真,那么方法getIcon
将返回较大的图像,而较小的图像则返回f参数为false?因此:返回ShellFolder.getShellFolder(新文件(文件名)).getIcon(true);
将返回具有最佳分辨率的图像?准确地说。这是您可以获得的最佳分辨率。自以前以来,分辨率肯定有所提高。谢谢。