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

The following examples show how to use org.jivesoftware.smackx.packet.VCard#load() . 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: XmppConnection.java    From weixin with Apache License 2.0 5 votes vote down vote up
/**
 * 获取用户VCard信息
 * 
 * @param connection
 * @param user
 * @return
 * @throws XMPPException
 */
public VCard getUserVCard(String user) {
	if (getConnection() == null)
		return null;
	VCard vcard = new VCard();
	try {
		vcard.load(getConnection(), user);
	} catch (XMPPException e) {
		e.printStackTrace();
	}
	return vcard;
}
 
Example 3
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 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;
}
 
Example 5
Source File: XmppVcard.java    From yiim_v2 with GNU General Public License v2.0 4 votes vote down vote up
public void save(Connection connection) {
	try {
		VCard vCard = new VCard();
		vCard.load(connection, mUserId);

		if (!YiUtils.isStringInvalid(mNickName)) {
			vCard.setNickName(mNickName);
		}

		if (!YiUtils.isStringInvalid(mGender)) {
			vCard.setField(Const.SEX, mGender);
		} else {
			vCard.setField(Const.SEX, Const.FEMALE);
		}

		if (!YiUtils.isStringInvalid(mSign)) {
			vCard.setField(Const.SIGN, mSign);
		}

		if (!YiUtils.isStringInvalid(mCountry)) {
			vCard.setField(Const.COUNTRY, mCountry);
		}

		if (!YiUtils.isStringInvalid(mProvince)) {
			vCard.setField(Const.PROVINCE, mProvince);
		}

		if (!YiUtils.isStringInvalid(mAddress)) {
			vCard.setField(Const.ADDRESS, mAddress);
		}

		vCard.setField(Const.BIRTHDAY, String.valueOf(mBirthday));
		vCard.setField(Const.SECOND_BIRTHDAY,
				String.valueOf(mSecondBirthday));
		vCard.setField(Const.ONLINETIME, String.valueOf(mOnlineTime));

		if (!YiUtils.isStringInvalid(mRealName)) {
			vCard.setField(Const.REALNAME, mRealName);
		}

		if (!YiUtils.isStringInvalid(mBloodGroup)) {
			vCard.setField(Const.BLOOD_GROUP, mBloodGroup);
		}

		if (!YiUtils.isStringInvalid(mPhone)) {
			vCard.setField(Const.PHONE, mPhone);
		}

		if (!YiUtils.isStringInvalid(mOccupation)) {
			vCard.setField(Const.OCCUPATION, mOccupation);
		}

		if (!YiUtils.isStringInvalid(mEmail)) {
			vCard.setField(Const.EMAIL, mEmail);
		}

		if (mAvatar != null && mAvatar.length > 0) {
			vCard.setAvatar(mAvatar);
			YiStoreCache.cacheRawData(mUserId, mAvatar);
		}

		vCard.save(connection);
	} catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
	}

	saveToDatabase();
}
 
Example 6
Source File: XmppManager.java    From weixin with Apache License 2.0 2 votes vote down vote up
/**
 * 获取用户VCard信息
 * 
 * @param user
 * @return
 * @throws XMPPException
 */
public VCard getUserVCard(String user) throws XMPPException {
	VCard vCard = new VCard();
	vCard.load(getConnection(), user);
	return vCard;
}