直接上代码:
/**
* 上传头像
*/
private void setUserImage(final XMPPConnection connection,
final byte[] image) throws XMPPException {
final VCard card = new VCard();
card.load(connection);
new Thread() {
@Override
public void run() {
try {
PacketFilter filter = new AndFilter(new PacketIDFilter(
card.getPacketID()), new PacketTypeFilter(IQ.class));
PacketCollector collector = connection
.createPacketCollector(filter);
String encodeImage = StringUtils.encodeBase64(image);
card.setAvatar(image, encodeImage);
card.setEncodedImage(encodeImage);
card.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"
+ encodeImage + "</BINVAL>", true);
Log.i("other", "上传头像的方法!");
card.save(connection);
IQ iq = (IQ) collector.nextResult(SmackConfiguration
.getPacketReplyTimeout());
if (iq != null && iq.getType() == IQ.Type.RESULT) {
Message msg = handler.obtainMessage();
msg.what = 2;
handler.sendMessage(msg);
}
} catch (XMPPException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}