Java Code Examples for org.jivesoftware.smackx.packet.VCard#getAvatar()

The following examples show how to use org.jivesoftware.smackx.packet.VCard#getAvatar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmppConnection.java    From weixin with Apache License 2.0 6 votes vote down vote up
/**
 * 修改用户头像
 * 
 * @param file
 */
public boolean changeImage(File file) {
	if (getConnection() == null)
		return false;
	try {
		VCard vcard = new VCard();
		vcard.load(getConnection());

		byte[] bytes;

		bytes = getFileBytes(file);
		String encodedImage = StringUtils.encodeBase64(bytes);
		vcard.setAvatar(bytes, encodedImage);
		vcard.setEncodedImage(encodedImage);
		vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>" + encodedImage + "</BINVAL>", true);

		ByteArrayInputStream bais = new ByteArrayInputStream(vcard.getAvatar());
		FormatTools.getInstance().InputStream2Bitmap(bais);

		vcard.save(getConnection());
		return true;
	} catch (Exception e) {
		e.printStackTrace();
		return false;
	}
}
 
Example 2
Source File: XmppManager.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 修改用户头像
 * 
 * @param f
 * @throws XMPPException
 * @throws IOException
 */
public void changeUserImage(File f) throws XMPPException, IOException {
	VCard vCard = new VCard();
	vCard.load(getConnection());
	byte[] bytes = getFileBytes(f);

	String encodeImage = StringUtils.encodeBase64(bytes);
	vCard.setAvatar(bytes, encodeImage);
	vCard.setEncodedImage(encodeImage);
	vCard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>" + encodeImage + "</BINVAL>", true);

	ByteArrayInputStream bais = new ByteArrayInputStream(vCard.getAvatar());
	FormatTools.getInstance().InputStream2Bitmap(bais);
	vCard.save(getConnection());
}
 
Example 3
Source File: XSCHelper.java    From PracticeCode with Apache License 2.0 4 votes vote down vote up
/**
 * 获取用户头像
 *
 * @param dir  可null。非空则将头像输出在文件中
 * @param pref 可null。非空,且需要更新头像则将头像唯一标识更新在配置文件中
 * @return Drawable 头像。如操作成功返回新的头像Drawable。否则返回null
*/
public Drawable getAvatar(File dir, SharedPreferences pref) {
    try {
        VCard card = getVCard();
        //如果无法下载名片或者没有头像则返回null
        if (card == null || card.getAvatar() == null) {
            return null;
        }

        //如果有配置,没有必要更新则不更新
        if (pref != null) {
            if (card.getField(AVATAG).equals(pref.getString(AVATAG, ""))) {
                if(dir != null && dir.exists())
                    return null;
            }
        }

        //如有dir,更新文件
        if(dir != null){
            FileOutputStream out = new FileOutputStream(dir);
            Bitmap image = BitmapFactory.decodeByteArray(
                    card.getAvatar(), 0, card.getAvatar().length);
            //压缩成功则输出,失败返回null
            if(image.compress(Bitmap.CompressFormat.JPEG, 100, out)) {
                out.flush();
                out.close();
            }
            else {
                return null;
            }
        }

        //如有配置,更新配置
        if(pref != null){
            pref.edit().putString(AVATAG, card.getField(AVATAG)).apply();
        }

        return Drawable.createFromStream(new ByteArrayInputStream(card.getAvatar()), null);
    } catch (IOException e) {
        return null;
    }
}
 
Example 4
Source File: XmppVcard.java    From yiim_v2 with GNU General Public License v2.0 4 votes vote down vote up
public static XmppVcard loadByVcard(Context context, Connection connection,
		String user, XmppVcard xmppVcard) {
	XmppVcard ret = null;
	if (xmppVcard != null) {
		ret = xmppVcard;
	} else {
		ret = new XmppVcard(context);
	}
	try {
		VCard vCard = new VCard();
		vCard.load(connection, user);

		Roster roster = connection.getRoster();

		ret.setNickName(vCard.getNickName());

		// 在线状态获取
		Presence presence = roster.getPresence(user);
		if (presence.getType() != Type.unavailable) {
			ret.setPresence("online");
		} else {
			ret.setPresence("unavailable");
		}

		// 加载用户头像
		byte[] avatar = vCard.getAvatar();
		if (avatar != null) {
			YiStoreCache.cacheRawData(user, avatar);
		}

		ret.setGender(vCard.getField(Const.SEX));
		ret.setSign(vCard.getField(Const.SIGN));
		ret.setCountry(vCard.getField(Const.COUNTRY));
		ret.setProvince(vCard.getField(Const.PROVINCE));
		ret.setAddress(vCard.getField(Const.ADDRESS));
		ret.setBirthday(Long.valueOf(vCard.getField(Const.BIRTHDAY)));
		ret.setSecondBirthday(Long.valueOf(vCard
				.getField(Const.SECOND_BIRTHDAY)));
		ret.setOnlineTime(Long.valueOf(vCard.getField(Const.ONLINETIME)));
		ret.setRealName(vCard.getField(Const.REALNAME));
		ret.setBloodGroup(vCard.getField(Const.BLOOD_GROUP));
		ret.setPhone(vCard.getField(Const.PHONE));
		ret.setOccupation(vCard.getField(Const.OCCUPATION));
		ret.setEmail(vCard.getField(Const.EMAIL));

		//通知重新加载好友列表
		Intent intent = new Intent(Const.NOTIFY_RELOAD_ROSTER_ENTRIES);
		context.sendBroadcast(intent);
	} catch (Exception e) {
	}

	return ret;
}