java 安全性不允许我使用 Jfilechooser 打开拍照

java security not allowing me to open take pics using Jfilechooser

我正在制作一个应用程序,其中构造函数需要一张图片。图片显然是使用 JFileChooser 选择的,然后显示在 JLabel 上。我的问题是我没有访问图片的安全权限。我测试了看我是否确实获得了绝对路径以及文件是否存在并且我确实获得了路径并且后者为真。那么我如何才能让我的应用访问至少只是获取图片?

我的代码

JFileChooser chooser = new JFileChooser();
            chooser.setFileFilter(new FileNameExtensionFilter("jpg","png"));
            int returnVal = chooser.showOpenDialog(diag);
            if(returnVal == JFileChooser.APPROVE_OPTION) {
                weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));
                weaponPic.setIcon(weaponImg);
                weaponPic.revalidate();
                weaponPic.repaint();

我的错误

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at ui.TempDialogs.mouseClicked(TempDialogs.java:171)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

java security not allowing me ..

这与安全无关,尽管堆栈跟踪在某些行中提到了 'security'。真正的问题是在堆栈跟踪的最顶部,..

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

我希望这一切都从这行代码开始..

weaponImg = new ImageIcon(TempDialogs.class.getResource(chooser.getSelectedFile().getAbsolutePath()));

这是错误的,而且不必要地令人费解。

  1. 获取资源用于从应用程序 class 路径上的资源生成 URL,对于文件既不需要也无用。
  2. 所以在这种尝试访问文件的情况下,我们可以使用 File 对象,或者 String 代表文件上文件的路径系统。所以它可以缩短为:

    weaponImg = new ImageIcon(chooser.getSelectedFile().getAbsolutePath()); // use String 
    
  3. 但如前所述,它也可以作为普通文件加载 File,因此这也可以:

    weaponImg = new ImageIcon(chooser.getSelectedFile()); // use File!