1
我正在创建一个聊天应用程序,但我无法理解如何使用smack API在openfire服务器上设置用户头像。我是下面的代码来设置用户头像。如何在openfire用户设置用户头像smack api
public boolean changeImage(File file) {
if (mConnection == null)
return false;
try {
VCard vcard = new VCard();
String userJID = prefs.getString(Prefrences.xmpp_jid, null);
System.out.println("user:- "+userJID);
vcard.load(mConnection, userJID);
byte[] bytes;
bytes = getFileBytes(file);
String encodedImage = StringUtils.encodeHex(bytes);
vcard.setAvatar(bytes, encodedImage);
vcard.setEncodedImage(encodedImage);
vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"
+ encodedImage + "</BINVAL>", true);
System.out.println("Encoded image "+encodedImage);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++");
ByteArrayInputStream bais = new ByteArrayInputStream(
vcard.getAvatar());
FormatTools.getInstance().InputStream2Bitmap(bais);
vcard.save(mConnection);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* File to byte
*
* @param file
* @return
* @throws java.io.IOException
*/
private byte[] getFileBytes(File file) throws IOException {
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
int bytes = (int) file.length();
byte[] buffer = new byte[bytes];
int readBytes = bis.read(buffer);
if (readBytes != buffer.length) {
throw new IOException("Entire file not read");
}
return buffer;
} finally {
if (bis != null) {
bis.close();
}
}
}
请帮忙。
Thaks您的回复。我已经尝试过,但它不适用于我的项目。我在服务器上没有映像。我只有位图对象,当我以字节转换我的位图对象时,未在openfire服务器上设置的头像。在此先感谢 –
不要将图像转换为位图,然后bytes.just设置图像的路径 – dipali