简体   繁体   English

无法从eclipse插件项目捆绑中获取图像文件URL

[英]Unable to get Image file URL from eclipse plugin project Bundle

I am trying to get image from a bundle in eclipse plugin project. 我正在尝试从eclipse插件项目中的捆绑包中获取图像。 But it always return null. 但是它总是返回null。 Please find the project structure below. 请在下面找到项目结构。 I want to get the URL of an image "icon_4.png" which is under "images/EmailTemplatesOutput/assets/icon_4.png". 我想获取“ images / EmailTemplatesOutput / assets / icon_4.png”下的图像“ icon_4.png”的URL。 I have tried different ways. 我尝试了不同的方法。 But it returns null. 但它返回null。

Project Structure is : 项目结构为: 在此处输入图片说明

            String path = "icon_4.png";
            Bundle bundle = Platform.getBundle("BulkDemo");
            URL url = FileLocator.find(bundle, new org.eclipse.core.runtime.Path(path), null);
            ImageDescriptor imageDesc = ImageDescriptor.createFromURL(url);
            Image image = imageDesc.createImage();

Don't put the 'images' directory in the 'src' folder, put it at the top level of the project (like 'bin' and 'META-INF'). 不要将“ images”目录放在“ src”文件夹中,而是将其放在项目的顶层(例如“ bin”和“ META-INF”)。 Make sure you update the 'build.properties' to include the images folder in the build. 确保更新“ build.properties”以在构建中包括images文件夹。

The path for the image is relative to the plug-in root so it will be images/EmailTemplatesOutput/assests/icon_4.png (assuming you move the images directory). 图像的路径是相对于插件根目录的,因此它将是images/EmailTemplatesOutput/assests/icon_4.png (假设您移动了图像目录)。

The URL returned by FileLocator.find uses an Eclipse only scheme so can only be used by Eclipse APIs. FileLocator.find返回的URL使用仅Eclipse方案,因此只能由Eclipse API使用。 You can convert the URL to a normal file URL by adding: 您可以通过添加以下内容将URL转换为普通文件URL:

URL fileURL = FileLocator.toFileURL(url);

This may cause Eclipse to unpack your plug-in to a temporary location to access the files. 这可能会导致Eclipse将插件解压缩到一个临时位置以访问文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM