注册新用户时如何上传用户头像到服务器?
How can I upload user's profile picture to server when registering new user ?
我正在使用 XMPP (Openfire) 开发 android 应用程序。用户可以从该应用程序注册新帐户,他们可以在注册表单中设置他们的个人资料图片。我想知道如何将个人资料图片保存到 Openfire 服务器。
您可以使用为 Smack 4.1 提供的 vCard 方法。在用户编辑个人资料信息时加载用户的 vCard。然后,允许他们上传他们的头像。一旦他们保存它,您将位图转换为字节数组,然后将其发送以保存 vCard。这是一个例子:
// Let the user pick their avatar
Bitmap bitmap;
// Take the avatar and convert it into a byte array:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// 90 refers the the compression quality. For PNG, the quality is ignored
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] avatarByte = stream.toByteArray();
// Once you get the byte array from the image, set the byte array to the vCard avatar
vCard.setAvatar(avatarByte);
// Then you can save the vCard details
vCardManager.saveVCard(vCard);
希望对您有所帮助
我正在使用 XMPP (Openfire) 开发 android 应用程序。用户可以从该应用程序注册新帐户,他们可以在注册表单中设置他们的个人资料图片。我想知道如何将个人资料图片保存到 Openfire 服务器。
您可以使用为 Smack 4.1 提供的 vCard 方法。在用户编辑个人资料信息时加载用户的 vCard。然后,允许他们上传他们的头像。一旦他们保存它,您将位图转换为字节数组,然后将其发送以保存 vCard。这是一个例子:
// Let the user pick their avatar
Bitmap bitmap;
// Take the avatar and convert it into a byte array:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// 90 refers the the compression quality. For PNG, the quality is ignored
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] avatarByte = stream.toByteArray();
// Once you get the byte array from the image, set the byte array to the vCard avatar
vCard.setAvatar(avatarByte);
// Then you can save the vCard details
vCardManager.saveVCard(vCard);
希望对您有所帮助