powered by Jive Software

.setAvatar()?

Hi all!

This is my first post and i am pretty new to smack so i apologise if this issue is a schoolboy error on

my part.

basically, i want to add an avatar to a user’'s vCard so i tried:

URL urldefault=new URL(“http://www.bbc.co.uk/tv/d/images/thumbnails/hp_jeremy_clarkson.jpg”);

VCard vCard = new VCard();

vCard.setJabberId(UserName);

vCard.setAvatar(urldefault);

vCard.save(connection);

However this failed and spat out the error

java.lang.NullPointerException

at org.jivesoftware.smack.util.StringUtils.encodeBase64(StringUtils.java:293)

at org.jivesoftware.smack.util.StringUtils.encodeBase64(StringUtils.java:282)

at org.jivesoftware.smackx.packet.VCard.setAvatar(VCard.java:330)

Next, i tried the other approach suggested:

URL urldefault=new URL(“http://www.bbc.co.uk/tv/d/images/thumbnails/hp_jeremy_clarkson.jpg”);

byte[] avatar = VCard.getBytes(urldefault);

VCard vCard = new VCard();

vCard.setJabberId(UserName);

vCard.setAvatar(avatar);

vCard.save(connection);

This failed too and produced the same error as before. I have been hacking away at this

for ages now but keep stalling on the setAvatar() bit.

Any help would be greatly appreciated.

Thanks,

Steven

You have to download the avatar from that URL first, convert it to jpg (due to a bug in Smack 2.2.1) if it isn’'t one already, store the result in a byte-array, and then pass that to setAvatar().

Hi, Anlumo

Thanks for your response.

was i not converting the jpg into a byte array with the statement

byte[] avatar = VCard.getBytes(urldefault);

?

Would you be able to display a small snippet of your solution as i have retried this again and am still getting nulls

Thanks

Steven

I checked the source, getBytes() only works for file URLs (bonus points for the good documentation). You have to get the data in a different way.

I haven’'t tried it, but maybe this will work:

import java.net.URL;
import java.io.InputStream; {
URL url = new URL("...");
InputStream stream = url.openStream();
...
}

To get the result of the input stream as a byte array, see here.

Hi anlumo

Your solution worked!

Thanks for your help

Steven