2013-05-20 233 views
2

我想將「icon」字符串轉換爲字節數組,並將其轉換爲位圖。問題是模擬器中的圖像不顯示。我想我做得不對,但我知道爲什麼。我將非常感謝您的幫助。在此先感謝Android - 將字符串轉換爲字節[]

這是我的JSON數據:

{ 
    "project": [ 
     { 
      "abbreviation": "abd", 
      "customer": "customer1", 
      "description": "description1", 
      "icon": "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC4UlEQVR42sXTXUhTYRgH8NNNXkTdmPlRpltqalgXZlB4hCC6C8VyYjErtbCoNLJiqQmOtqmYCkUXdhNEGSHYuVXCj81N58dcO+rOPt3c2dnOWTubeSPUv6PZx1U3XfTAj4f3gf/zXL0E8d+LoqgEw+RkvnnOWmi22H6Ytx4bHRtLSt2XSKhUqr8vkMKFYS5I8y5LhGemBYEx8xHPp3CICxpoevFcf3//LrVanaDRaBK0Wu0WnU632XdK8x3E1MxcSZR1Cez7ewgM1IEdqEWYasW6yCMajQqBQGDE5/N98Hq9lNvtppxOJ8UwDEXT9LuJiYmThMlkIuMxUVgXQ1KIw5doEGufOcRjMYiiiEgkAp7nwXEcWJaF3+/HysoKnC7XxtTUdA1hNBrJzUvx+Bpi8Thisbh0+XcwFAohyIUQYANS0A2P2wGXyw6GWdyYMo7VEXq9nuR5QSpeCnDgw0GEQ6x0MQA24MeqzwPvwhiWXzeD7q6CrecibL2XYO2s+DrzoGCEGNcbilmfk3e8fYSlXiWWn13F8vMaLD27gsU+JWydFbDcLcRsVTJmFEmYqdym2AuzcreDGNWbij02A79w/wTmlOmYqz74y6zypwzMVmdi9vIfqjNgrk0cJz5OmIoZI8Vb7kkLruVK8rblbpm/dhjz1yX1ObBsuvHD5tt8K3WQGB43FtPDr3hLYxEW6o/CWl8A280jWLydh+XGHDBNWXA+lMOpksHVnAlnSyYcrTLYW+SwtqS9kBZMkrahPoG+Uwh7w1G4m/LgV2Uh2CZDWJ0BQZOOSMd+RDpTIXSlgJdwnWlY7TjwjdGmtRFvBoeK9C9b7fMNx2J0U67oeHRI9D6WiavtcjH4JEvktBKdXJIusrpU0a9NFj2aFNHefiBsUGVcJk6fObunqbq0SF2aX6IpyyY7yrPIrvPZZHdFDtmjyCV7K/O3+lOFNFPIya4KGdlxQUa2l8lPlR9PS/nnz/gdnPcTQcpv7vgAAAAASUVORK5CYII=", 
      "name": "projectname1", 
      "plannedEndDate": "2012-05-25T00:00:00+01:00", 
      "plannedStartDate": "2012-05-23T00:00:00+01:00", 
      "projectStatus": { 
       "name": "Opened" 
      }, 
      "realEndDate": "2012-05-25T00:00:00+01:00", 
      "realStartDate": "2012-05-23T00:00:00+01:00" 
     } 
    ] 
} 

我這樣做是這樣的,但它不工作:

// convert String to byte[] 
String stringToConvert = project.getIcon(); 
byte[] theByteArray = stringToConvert.getBytes(); 

// convert Byte[] to Bitmap 
Bitmap bm = BitmapFactory.decodeByteArray(theByteArray, 
     0, theByteArray.length); 
DisplayMetrics dm = new DisplayMetrics(); 

holder.imageIcon.setMinimumHeight(dm.heightPixels); 
holder.imageIcon.setMinimumWidth(dm.widthPixels); 
holder.imageIcon.setImageBitmap(bm); 

回答

6

從字符串icon看來你已經將您的圖像串通過使用Base64。所以,你必須使用Base64編碼本身

// Receiving side 
byte[] data = Base64.decode(iconString, Base64.DEFAULT); 

希望這有助於將其轉換回字節...

+0

非常感謝它完美:) – Wijden

+0

非常感謝你......解決了我的2個一天的problem..Thanks很多的答案.. – Lal

1

你的圖標是Base64編碼,使用Base64 class進行解碼。

2

解譯您的base64stringbyte[]

byte[] imgBytes = Base64.decode(strBase64.getBytes()); 

要轉換byte[]Bitmap

Bitmap bitmap = BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.length) 

現在使用bitmap與您的圖像的setImageBitmap方法來顯示輸出。例如:

icon.setImageBitmap(bitmap); 
+0

非常感謝你,但我有這樣做它會得到en錯誤,當我這樣做,就像你說:byte [] imgBytes = Base64.decode(strBase64.getBytes(),Base64.DEFAULT); – Wijden

相關問題