如何在openfire用户smack api上设置用户头像

时间:2016-10-24 04:22:23

标签: android xmpp openfire asmack avatar

我正在创建一个聊天应用程序,但我无法理解如何使用smack api在openfire服务器上设置用户头像。我是以下代码来设置用户头像。

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();
        }
    }
}

请帮助。

1 个答案:

答案 0 :(得分:0)

使用此代码:它可能对您有帮助:))

private void loadVCard(XMPPConnection conn, String username) {
    VCard vcard = new VCard();

    //ProviderManager.addIQProvider("vCard", "vcard-temp", new VCardProvider());



    vcard.load(conn, username);


    vcard.setFirstName("" + username);
    vcard.setEmailHome("" + email);
    vcard.setMiddleName("" + middleName);
    vcard.setNickName("" + nickName);
    vcard.setPhoneHome("Voice", "" + phoneNumber);
    vcard.setLastName("" + lastName);
    vcard.setOrganization("" + orginiZation);
    vcard.setAvatar("" + image_path); //Image Path should be URL or Can be Byte Array etc.


        vcard.save(conn);

}