Search code examples
androidxmppopenfiresmackasmack

How can I upload user's profile picture to server when registering new user ?


I am developing an android app using XMPP (Openfire) . User can register new account from that app and they can set their profile picture in register form. I want to know how can I save that profile picture to Openfire server.


Solution

  • You can use the vCard method that is given for Smack 4.1. Load the user's vCard when they are editing their profile information. Then, allow them to upload their avatar. Once they save it, you convert the Bitmap to a byte array, which is then sent to save vCard. Here's an example:

    // 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);
    

    Hope that helps