Java 提取应用程序图标仍然为空
我试图从文件夹中提取图标图像的Java 提取应用程序图标仍然为空,java,icons,imageicon,Java,Icons,Imageicon,我试图从文件夹中提取图标图像的arraylist,但我一直得到一个NullPointerException。我已经可以提取较小的版本,但它们太小了。我试图得到的图标是常规大小的图标文件路径保存图标位置列表iconBIG.add(…)是NullPointerException错误所指向的位置 // Global private ArrayList<Icon> iconBIG = new ArrayList<Icon>(); // Within extractIcon().
arraylist
,但我一直得到一个NullPointerException。我已经可以提取较小的版本,但它们太小了。我试图得到的图标是常规大小的图标<代码>文件路径保存图标位置列表iconBIG.add(…)
是NullPointerException
错误所指向的位置
// Global
private ArrayList<Icon> iconBIG = new ArrayList<Icon>();
// Within extractIcon()...
for (String target : filePaths)
{
try
{
ShellFolder shell = ShellFolder.getShellFolder(new File(target));
iconBIG.add(new ImageIcon(shell.getIcon(true)));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
//全局
private ArrayList iconBIG=new ArrayList();
//在extractIcon()中。。。
for(字符串目标:文件路径)
{
尝试
{
ShellFolder shell=ShellFolder.getShellFolder(新文件(目标));
添加(新的ImageIcon(shell.getIcon(true));
}
catch(filenotfounde异常)
{
e、 printStackTrace();
}
}
编辑:我已经拥有使用ShellFolder的完全权限
更新:如果我更改,它会显示新文件(目标)
(仅保存应用程序的完整路径)
getShellFolder(新文件(目标)
到
getShellFolder(新文件(C:/foo/bar.lnk)
代码可以工作。我之前已经想到用“/”替换所有
\
,但我不明白为什么它仍然调用相同的错误。似乎可以工作,因为您允许执行此私有包。下面是一个在JDK 6上工作的示例:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import javax.swing.*;
import sun.awt.shell.ShellFolder;
public class ShowShellIcon {
private static void createAndShowUI() {
final JFrame frame = new JFrame("Load Image");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton loadButton = new JButton("Display Image");
loadButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser(
System.getProperty("user.home"));
if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION){
try {
ShellFolder shell = ShellFolder.getShellFolder(fc
.getSelectedFile());
JOptionPane.showMessageDialog(null,
new ImageIcon(shell.getIcon(true)));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
});
frame.add(loadButton);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
createAndShowUI();
}
});
}
}
iconBIG
在这个代码块之前初始化了吗?@Abu是的。它是globalWhat's ShellFolder?它不是启动卡JDK的一部分(据我所知)@在Eclipse中,Window->Preferences->Java->Compiler->Error Message->Disprecated and Restricted API->set Forbidded Reference to Ignore是否检查了shell
和iconBIG
是否为空?问题是它只显示文件夹,而不显示文件夹和文件。这不是一个问题,它是如果只筛选文件夹,我删除了该行-fc.setFileSelectionMode(仅JFileChooser.DIRECTORIES_);
。它现在显示文件。请参阅更新的代码。