2013-07-28 40 views
2

我有一個Web服務,它給了我一個名爲'imagedata'節點的JSON。它包含一個龐大的數據作爲字符串。當我在瀏覽器中打印時,它提供了有效的輸入。 Base64編碼的字符串以'='字符結尾。Android - 龐大的數據在基地64,如何處理它

我也在html頁面中使用這個標籤測試過它,它工作得很好。

<img src="data:image/png;base64,MY_BASE64_ENCODED_STRING"/> 

這是我的代碼;

StringBuilder b64 = new StringBuilder(dataObj.getString("imagedata")); 
byte[] decodedByte = Base64.decode(b64.toString(), 0); 
bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 

請注意,此代碼適用於更小的圖像數據,但給出更大的圖像數據

壞的base64例外,請幫我出, 感謝

+0

任何異常stacktrace? –

+0

會建議使用Base64.decode(dataObj.getString(「imagedata」),0);直接 – KOTIOS

+0

@ Stacks28仍然收到相同的錯誤。 java.lang.IllegalArgumentException:bad base-64 – umirza47

回答

0

爲什麼你的服務器給你base64編碼? Base64只是通信而不是編碼圖像。如果它用於編碼,它會使圖像文件變得更大.IllegalArgumentException意味着圖像編碼格式不正確,否則無法解碼。 在我的項目中,我只是,現在,我使用Base64發送圖像。但它會由多部分改變。但是,當服務器轉發給收件人。它只是轉發圖像的網址。所以我可以做簡單的過程鏈接到圖片與此:

public static Image loadImage(String url) 
{ 
    HttpConnection connection = null; 
    DataInputStream dis = null; 
    byte[] data = null; 

    try 
    { 
     connection = (HttpConnection) Connector.open(url); 
     int length = (int) connection.getLength(); 
     data = new byte[length]; 
     dis = new DataInputStream(connection.openInputStream()); 
     dis.readFully(data); 
    } 
    catch (Exception e) 
    { 
     System.out.println("Error LoadImage: " + e.getMessage()); 
     e.printStackTrace(); 
    } 
    finally 
    { 
     if (connection != null) 
      try 
      { 
       connection.close(); 
      } 
      catch (IOException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     if (dis != null) 
      try 
      { 
       dis.close(); 
      } 
      catch (IOException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 


    return Image.createImage(data, 0, data.length); 
} 

注意此代碼爲J2ME。