2015-05-04 44 views
1

我试图使用SWT Image如何从插件内部资源获取图像?

Image image=new Image(device,"icons/imagename.png"); 

的图标文件夹是在插件的类路径,但我仍然得到了IOException摆脱我的插件资源的图像文件。

你能帮我指出什么是从插件的内部资源访问图像资源的正确方法?

+0

你在使用'java.awt.Image'吗? – Blip

回答

3

使用类似:

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(); 

不要忘了,当你用它做,你必须处理的图像。

+0

FWIW:将图像复制到您自己的插件中可能更好,以防从未来版本的插件中移除图像。 –

+0

@david当图像位于您自己的插件中时,您仍然使用相同的代码。 –

+0

非常真实。我只是警告不要依赖另一个可能在将来会改变的插件中的资源。 –