cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Set avatar for ThingTemplate in custom extension?

sclifford
1-Newbie

Set avatar for ThingTemplate in custom extension?

Hi,

I want to set the avatar for a ThingTemplate, and all implementing Things, in my custom extension. There's not much on this in the Extension Development Guide (v4).

Currently, I'm attempting to use SetAvatar in the initializeThing() method of my extensions ThingTemplate.

@Override

public void initializeThing() throws Exception {

     super.initializeThing();

    InputStream stream = this.getClass().getClassLoader().getResourceAsStream(resourceName);

   if (stream == null) {

       throw new Exception("Cannot get resource \"" + resourceName + "\" from Jar file.");

   }

    byte[] content = Base64.encode(IOUtils.toByteArray(stream));

   this.setAvatar(content);

}

resourceName is the name of a PNG image located in the root of my extension project directory.

Is their annotation or aspect I should be setting?

Thanks,

Shane

14 REPLIES 14
mhollenbach
5-Regular Member
(To:sclifford)

Shane,

What happens when the code you cited above is exercised?

Meghan

Meghan,

Thanks for getting back to me.
Nothing happens; no exception is thrown and the code after (which retrieves values from the configuration table) runs as expected. However, the avatar image is not set for the ThingTemplate or initialised Thing in the Composer.
I will try open up a debug port on my local Tomcat instance and step through the code from my Eclipse IDE.
Shane

I don't know if that's the case, but sometimes you can have Cache Problems with ThingTemplate's Avatar, did you tried to refresh Composer browser tab with shift key pressed?

Thanks for the suggestion. Unfortunately, shift+refresh didn't solve it.

mhollenbach
5-Regular Member
(To:sclifford)

Shane,

The code I used was this:

@Override

  public void initializeThing() throws Exception {

  super.initializeThing();

  Path path = Paths.get("D:\\Users\\mhollenbach\\Pictures\\werd_2014.gif");

  byte[] content = Files.readAllBytes(path);

  this.SetAvatar(content);

  }

This is basically what you have, I'm just setting it from an image on my file system. This works when you create a Thing from that Template, but on the Template itself I'm seeing this (a white circle where the picture should be):

avatar_whitecircle.PNG

On the Thing I created I am seeing the image:

avatar_picture.PNG

Maybe try obtaining your image differently and that will work. I'll keep investigating what's going on with the ThingTemplate though.

Meghan

Thanks Meghan,

I tried that and specifying the image from a local file path works as you described. But still no avatar for the ThingTemplate.

However, this approach won't work unless the Thingworx instance is installed on the same local system, right? For example, if I import the extension on an instance on a different machine, where the image file doesn't exist.

There are lots of extensions on the Thingworx Marketplace with custom avatars ... are there any source examples available?

Shane

The stream variable was 'null' until I modified the following line of code:

InputStream stream = this.getClass().getResourceAsStream(resourceName);

where:

String resourceName = "my-icon.png";

Also both the *.class files for my extension project and the PNG image are located in the Jar file under the same package.

However, as I step through the code in the IDE a NullPointerException is thrown by:

this.SetAvatar(content);

?

If you look at your code, Meghan it's using .SetAvatar and Shane it's using .setAvatar, that lower case can be the clue...

That was a typo ... my bad. In the actual code it is .SetAvatar

Hi Shane,

Just download the extensions which does it, and look onto the code how they do it

Carles.

mhollenbach
5-Regular Member
(To:sclifford)

Ok, new code. I switched from nesting it in the java package to placing it in the Entities directory for now.

@Override

  public void initializeThing() throws Exception {

  super.initializeThing();

  URL url = getClass().getResource("Entities/werd_2014.gif");

  File file = new File(url.getPath());

  FileInputStream fileInputStream;

  byte[] content = new byte[(int) file.length()];

       

    try {

        //convert file into array of bytes

    fileInputStream = new FileInputStream(file);

    fileInputStream.read(content);

    fileInputStream.close();

    } catch(Exception e){

          e.printStackTrace();

    }

  this.SetAvatar(content);

  }



​Now, this sets the Thing's Avatar, but on the General Information page of the Thing and ThingTemplate there is still a white circle, but you will see that the actual icon representing the Thing in Composer has been set.

Meghan

Thanks Meghan, I think that is the solution if setting the avatar from within the code.

Carles, so I downloaded and extracted an extension. No source code provided, but after decompiling the .class file, I discovered the avatar is not being set in code.

Avatar is set in the metadata.xml file for the extension; by adding an <avatar> tag to the <ThingTemplate> tag. Thing instances inherit the Avatar of their base Template.

<Entities>

    ...

    <ThingTemplates>

        <ThingTemplate dataChangeEventsEnabled="true" description="" iconURL="" name="myExtension" tags="" thingPackage="MyExtensionThing">

            <avatar>

                iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI...

            </avatar>

        </ThingTemplate>

    </ThingTemplates>

</Entities>

Thank you both for your help.

mhollenbach
5-Regular Member
(To:sclifford)

Shane,

That is a good alternative solution, but the avatar should still be able to be set in the Java code. I'm speaking with a developer right now about this and will post on this thread once I get this resolved. In the meantime, if that workaround works for you, fantastic !

Meghan

Sorry to bump this old thread, but do we have a solution on how to set the Avatar from Java extension SDK. The above code snippets work on absolute paths, but not for relative paths.

Top Tags