2016-10-24 50 views
1

我正在創建一個聊天應用程序,但我無法理解如何使用smack API在openfire服務器上設置用戶頭像。我是下面的代碼來設置用戶頭像。如何在openfire用戶設置用戶頭像smack api

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

請幫忙。

回答

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

} 
+0

Thaks您的回覆。我已經嘗試過,但它不適用於我的項目。我在服務器上沒有映像。我只有位圖對象,當我以字節轉換我的位圖對象時,未在openfire服務器上設置的頭像。在此先感謝 –

+0

不要將圖像轉換爲位圖,然後bytes.just設置圖像的路徑 – dipali