Java 如何从插件内部资源获取图像?

Java 如何从插件内部资源获取图像?,java,eclipse,eclipse-plugin,swt,Java,Eclipse,Eclipse Plugin,Swt,我正试图使用SWTimage从我的插件资源中获取一个图像文件 Image image=new Image(device,"icons/imagename.png"); 图标文件夹在插件的类路径中,但我仍然得到一个IOException 您能帮我指出从插件的内部资源访问图像资源的正确方式吗?使用以下方法: String path = "icons/imagename.png"; Bundle bundle = Platform.getBundle("plugin id"); URL url =

我正试图使用SWT
image
从我的插件资源中获取一个图像文件

Image image=new Image(device,"icons/imagename.png");
图标文件夹在插件的类路径中,但我仍然得到一个
IOException

您能帮我指出从插件的内部资源访问图像资源的正确方式吗?

使用以下方法:

String path = "icons/imagename.png";
Bundle bundle = Platform.getBundle("plugin id");
URL url = FileLocator.find(bundle, new Path(path), null);
ImageDescriptor imageDesc = ImageDescriptor.createFromURL(url);
Image image = imageDesc.createImage();

不要忘记,在处理完图像后,您必须对其进行处理。

您使用的是
java.awt.image
?FWIW:最好将图像复制到您自己的插件中,以防该图像从插件的未来版本中删除。@david当图像在您自己的插件中时,您仍然使用相同的代码。非常正确。我只是提醒大家,不要依赖另一个插件中的资源,这可能会在将来发生变化。