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

时间:2017-05-31 05:27:21

标签: java swing user-interface nullpointerexception 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$5.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.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.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$4.run(Unknown Source)
at java.awt.EventQueue$4.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)

1 个答案:

答案 0 :(得分:2)

  

java安全不允许我..

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

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

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

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

这既错误又不必要的错综复杂。

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

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

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