If this is an RCP application, I would go with a scalable solution.
Create an ImageCache object that you create at the beginning of the application life cycle (preferably in the Activator class of the application).
This ImageCache can receive images (and, of course, cache them) from the path relative to the plugin (for example, the plugin has an icons folder, and then when you need an icon, you simply call Activator.getDefault().getImage("icons/random.png"); - where getDefault() is an instance of Activator singleton).
There are two of them in ImageCache :
public ImageDescriptor getImageDescriptor(final String path) { ImageDescriptor imgD = AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path); if (imgD == null) { return null;
and
public Image getImage(final String path) { Image image = imageCacheMap.get(path); if (image == null) { image = getImageDescripto(path).createImage(); imageCacheMap.put(path, image); } return image; }
Since these images need to be removed, use the dispose() method in ImageCache , which is called in the stop() method of the Activator .
There are many approaches to this. In my opinion, this is the best option for RCP applications.
source share