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

Image binary data from service code

jasong
Newbie

Image binary data from service code

I'd like to get the raw base64 data that is the Image from a Thing's image property.

 

Behind the scenes(in the REST JSON object) the image property appears to be a base64 string of the binary image data. But in the service code, it's different.

 

For example, you can't do this:

logger.warn("MyImageProeprty: " + Things['MyThing'].MyImageProperty);

the result of that is:  [B@780be821

 

I could of course get this data by snagging and then digging through the Thing's JSON from it's localhost REST endpoint, but that seems like more work than it should be. I suspect this Image object that I see in the service has some property or function on it to get this but trying to find that manually would take awhile. I have not seen anything in the Snippets either.

 

Thoughts?



6 REPLIES 6

Image binary data from service code

Hi Jason, I guess my first question would be, what would you like to do with the data?

Knowing that, we can probably guide you along.

Image binary data from service code

Oye, ok, didn't want to go into all of this but maybe we'll learn something.


I have a Thing called DefaultAvatars that has several image properties.


I have a service that creates ThingShapes, via a localhost call to the REST services that the UI uses. I send in a JSON payload and it creates it. (Once 5.1 Entity services come out with ThingShape creation, I won't need this.)


When I create these ThingShapes, I want to set their avatars to the image properties stored on DefaultAvatars.


To do that via the UI Services, I need the image data in it's semi-raw Base64 format.


The other option would be to use the ThingShape.SetAvatar service, once the ThingShape is created, but due to the transactional nature of these services, I have had many problems in the past trying to work with objects created outside of the service (via the UI services) and then accessing them in the same service call.  Just to cover my bases, I did try that as well:


ThingShapes[FeatureThingShapeName].SetAvatar( );


but received the following error:

JavaException: java.lang.Exception: Unable to decode avatar content: Invalid length.



Image binary data from service code

Hi Jason, I know these services didn't exist early on, but I recommend you use the CreateThingShape service which is available in our current 4.2 release. That should avoid the timing issues you are having and make it much easier to assign the Avatar.

You would still need to do the JSON PUT to get the Properties/Services/Events/Subscriptions in place as you do now.

Image binary data from service code

Yeah, I know that one is there, but due to those transaction/timing issues, until I can set properties, there is no real point in making a shape via that method. I would be able to set the avatar, true, but then not the properties. That localhost service callout to edit properties would be nearly identical to what I am already doing, plus, due to the transactional nature of services, it effectively would happen before the shape is created, which is of course impossible, until you guys implement that time travel feature. :-)


But let's address that error I got when trying it in the reverse order - 


ThingShapes[FeatureThingShapeName].SetAvatar( );

//.FeatureAvatar is of type Image


Error:

JavaException: java.lang.Exception: Unable to decode avatar content: Invalid length.


Why would that happen? Are Avatar and Image different?



Image binary data from service code

Hi Jason, that SetAvatar may have a bug in it, I'm getting Dev to look at it.

Image binary data from service code

Looks like a fix was put in for SetAvatar that will be available in the next release, hopefully that will solve this issue.

Announcements

Check out the upcoming Expert Session: Understanding ThingWorx Navigate Licensing in Community "Customer Events" section.