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