Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » load an image from within a plugin(cannot use the filelocator to find the image)
load an image from within a plugin [message #514084] Fri, 12 February 2010 06:41 Go to next message
Luca Ferrari is currently offline Luca Ferrari
Messages: 159
Registered: November 2009
Senior Member
Hi all,
I've got a plugin with an "img" directory listed in the classpath and from which I'd like to load an image (the application startup logo shown in a view) with the following code:

    static {
	try {

	    // load a dummy image and read each byte, then set the bytes as
	    // those for the
	    // dummy image
	    File dummyImage = new File(DEFAULT_LOGO_IMAGE_FILE_NAME);


	    // load the image thru the bundle facilities
	    if( ! dummyImage.isAbsolute() || ! dummyImage.exists() || ! dummyImage.isFile() ){
		Bundle bundle = Platform.getBundle( Activator.PLUGIN_ID ); 
		IPath imagePath = new Path( DEFAULT_LOGO_IMAGE_FILE_NAME );
		
		
		URL fileURL = FileLocator.find(bundle, imagePath, Collections.EMPTY_MAP);
		fileURL = FileLocator.toFileURL(fileURL);
		dummyImage = new File( fileURL.getPath() );

	    }
	    
	    InputStream is = new FileInputStream(dummyImage);
	    dummyImageBytes = new byte[is.available()];
	    is.read(dummyImageBytes);

	} catch (IOException e) {
	    // TODO Auto-generated catch block
	    e.printStackTrace();
	}

    }


The above code loads the bytes of the image (for further elaboration), however the problem is that the FileLocator throws a NullPointerException since the fileURL is always null. The searched image (DEFAULT_LOGO_IMAGE_FILE) has been set either to img/logo.png or /img/log.png. In either case the null pointer exception arises:

java.lang.NullPointerException
	at org.eclipse.core.internal.runtime.Activator.getURLConverter(Activator.java:313)
	at org.eclipse.core.runtime.FileLocator.toFileURL(FileLocator.java:205)
	at hrpm.rcp.gui.view.BaseImageView.<clinit>(BaseImageView.java:322)
	... 67 more


The line 322 is the following:

fileURL = FileLocator.toFileURL(fileURL);


The plugin.xml should not even contain the "img" directory as runtime classpath entry, since it is at the root of the plugin directory, right? So does anybody knows how can I solve the problem? (I've also tried to load the image thru a classloader as resource but without any success).

Re: load an image from within a plugin [message #514241 is a reply to message #514084] Fri, 12 February 2010 18:42 Go to previous messageGo to next message
Eugene  is currently offline Eugene
Messages: 25
Registered: July 2009
Junior Member
Have you tried loading the image using the Activator?

    private ImageDescriptor largeImageDescriptor
    	= Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID
 		   , "pathToYourResource"); 

Re: load an image from within a plugin [message #514402 is a reply to message #514241] Mon, 15 February 2010 04:09 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca Ferrari
Messages: 159
Registered: November 2009
Senior Member
No, it is not working.
Moreover I need a way to get the image bytes, not the raw image, that's why I'm trying to load the image file. Any idea?
Re: load an image from within a plugin [message #514412 is a reply to message #514402] Mon, 15 February 2010 04:19 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5294
Registered: July 2009
Senior Member
Why not getClass().getClassLoader().getResourceAsStream("/img/bla.png ")?

Tom

Am 15.02.10 10:09, schrieb Luca Ferrari:
> No, it is not working. Moreover I need a way to get the image bytes, not
> the raw image, that's why I'm trying to load the image file. Any idea?
Re: load an image from within a plugin [message #514433 is a reply to message #514412] Mon, 15 February 2010 05:32 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca Ferrari
Messages: 159
Registered: November 2009
Senior Member
It is throwing a null pointer exception, it is not getting the image:

 static {
	try {

		InputStream is = BaseImageView.class.getClassLoader().getResourceAsStream( "/img/logo.png" );
		is.read( dummyImageBytes );


This is a static initializer, but I don't believe the problem is there.
Re: load an image from within a plugin [message #514435 is a reply to message #514433] Mon, 15 February 2010 00:48 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5294
Registered: July 2009
Senior Member
Then the problem is that you img is not part of the Bundle-Path, how did
you configured it?

Tom

Am 15.02.10 11:32, schrieb Luca Ferrari:
> It is throwing a null pointer exception, it is not getting the image:
>
>
> static {
> try {
>
> InputStream is =
> BaseImageView.class.getClassLoader().getResourceAsStream(
> "/img/logo.png" );
> is.read( dummyImageBytes );
>
>
> This is a static initializer, but I don't believe the problem is there.
Re: load an image from within a plugin [message #514447 is a reply to message #514084] Mon, 15 February 2010 02:00 Go to previous messageGo to next message
Christian is currently offline Christian
Messages: 72
Registered: July 2009
Member
Hello
Have you tried something like
AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ ID,
"img/logo.png").createImage();

if that doesn't work the first thing I would check were PluginID in
activator still being correct.
Second thing would be the build.properties file if the image is in the
list of exported files.

Christian
Re: load an image from within a plugin [message #518247 is a reply to message #514447] Wed, 03 March 2010 10:21 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca Ferrari
Messages: 159
Registered: November 2009
Senior Member
I've checked and the img path is contained into the bundle classpath, should it be also somewhere else?

Bundle-ClassPath: classes/,
 org.springframework.beans-3.0.0.M4.jar,
 org.springframework.core-3.0.0.M4.jar,
 org.springframework.jdbc-3.0.0.M4.jar,
 org.springframework.transaction-3.0.0.M4.jar,
 commons-logging-1.1.1.jar,
 conf/,
 postgresql-8.3-603.jdbc4.jar,
 img/,
 org.springframework.aop-3.0.1.RELEASE-A.jar,
 aopalliance.jar



Moreover, if I load an image thru the ImageDescriptor how can I get the image bytes from it?
Re: load an image from within a plugin [message #518275 is a reply to message #518247] Wed, 03 March 2010 11:12 Go to previous messageGo to next message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Hi,

If img is in your bundle plugin root, you can simply use:
URL url = bundle.getEntry("/img/logo.png");
InputStream in = url.openStream();

This will work both in your workspace and when deployed, presuming your
binary entry in build.properties includes the /img directory.


With /img on your Bundle-ClassPath that implies that if you wanted to
call getResourceAsStream(*) you would need to use /logo.png, since /img
is part of the classpath, not on the classpath.

PW


--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Re: load an image from within a plugin [message #518288 is a reply to message #518275] Wed, 03 March 2010 12:00 Go to previous messageGo to next message
Luca Ferrari is currently offline Luca Ferrari
Messages: 159
Registered: November 2009
Senior Member
Thanks, at last I've got the image loaded and the bytes into the stream with your suggestions and the following piece of code:

                Bundle bundle = Platform.getBundle( hrpm.rcp.Activator.PLUGIN_ID );
		URL url = bundle.getEntry(DEFAULT_LOGO_IMAGE_FILE_NAME);
		URL fileURL = FileLocator.toFileURL( url );
		File dummyImage = new File( fileURL.getPath() );
		InputStream is = new FileInputStream(dummyImage);
		bytes = new byte[ is.available() ];
		is.read( bytes );
Re: load an image from within a plugin [message #518627 is a reply to message #518288] Thu, 04 March 2010 11:05 Go to previous message
Paul Webster is currently offline Paul Webster
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Luca Ferrari wrote:
> URL fileURL = FileLocator.toFileURL( url );
> File dummyImage = new File( fileURL.getPath() );
> InputStream is = new FileInputStream(dummyImage);

So what is this code for, exactly? That looks like a lot of extra work
(plus it's potentially cloning the file out into a temporary location on
disk).

Are you telling me that InputStream is = url.openStream(); did not
work? If that's the case, could you please open a bug.

> bytes = new byte[ is.available() ];
> is.read( bytes );
>

PW

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:Maintaining file rights for different platforms
Next Topic:Creating multiple Display objects
Goto Forum:
  


Current Time: Fri Aug 29 12:36:31 EDT 2014

Powered by FUDForum. Page generated in 0.02735 seconds