2013-07-06 98 views
18

我有一個圖像通過JSON字符串發送給我。我想在我的android應用程序中將該字符串轉換爲圖像,然後顯示該圖像。將base64字符串轉換爲Java中的圖像

JSON字符串看起來是這樣的:

"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..." 

注:我截斷了字符串...

我已經得到了(我認爲)將字符串轉換成圖像的功能。我做對了嗎?

public Bitmap ConvertToImage(String image){ 
    try{ 
     InputStream stream = new ByteArrayInputStream(image.getBytes()); 
     Bitmap bitmap = BitmapFactory.decodeStream(stream);     
     return bitmap; 
    } 
    catch (Exception e) { 
     return null;    
    } 
} 

然後我嘗試這樣

String image = jsonObject.getString("barcode_img");   
Bitmap myBitmap = this.ConvertToImage(image); 
ImageView cimg = (ImageView)findViewById(R.id.imageView1); 

//Now try setting dynamic image 
cimg.setImageBitmap(myBitmap); 

但是其顯示在我的Android活動,當我這樣做,什麼也不顯示。我沒有在logcat中出現任何錯誤。我究竟做錯了什麼?

感謝

回答

43

我很擔心,你需要僅解碼的base64字符串來獲取圖像的字節,所以在您的

"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVI..." 

字符串,必須data:image\/png;base64,後得到的數據,所以你只有圖像字節,然後對其進行解碼:

String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1); 

InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT)); 

這是一個代碼,以便您瞭解它是如何工作的,但如果你收到一個JSON對象,它應該做的正確方式

  • 將JSON字符串轉換爲JSON對象。
  • 提取data密鑰下的字符串。
  • 請確保以image/png開頭,因此您知道這是一張png圖片。
  • 確保包含base64字符串,因此您知道必須解碼數據。
  • 解碼base64字符串後的數據以獲取圖像。
+0

你搖滾!感謝索引爲「,」的子字符串...該部分是強制性的。 – user952342

+0

不客氣;) –

+0

正確的......這個工作對我來說.. –

3
InputStream stream = new ByteArrayInputStream(image.getBytes()); 

應改爲

InputStream stream = new ByteArrayInputStream(Base64.decode(image.getBytes(), Base64.DEFAULT)); 

參考http://developer.android.com/reference/android/util/Base64.html關於如何做Base64編碼解碼細節。

聲明:我沒有檢查語法,但這是你應該怎麼做。

+1

'image \/png; base64''不必解碼。 –

+1

@Jorge,而不是downvoting,你可以編輯我的答案。要點是作者缺少執行base 64解碼。 –

+0

感謝魔杖製造商!你的解決方案是一半的權利......但我確實需要在「,」後面加一個子字符串。 – user952342

1

以下是轉換Base64編碼的inputStream並將其寫入磁盤的工作代碼。 我花了一些時間使其正常工作。所以我希望這可以幫助其他開發者。

public boolean writeImageToDisk(FileItem item, File imageFile) { 
    // clear error message 
    errorMessage = null; 
    FileOutputStream out = null; 
    boolean ret = false; 
    try { 
     // write thumbnail to output folder 
     out = createOutputStream(imageFile); 

     // Copy input stream to output stream 
     byte[] headerBytes = new byte[22]; 
     InputStream imageStream = item.getInputStream(); 
     imageStream.read(headerBytes); 

     String header = new String(headerBytes); 
     // System.out.println(header); 

     byte[] b = new byte[4 * 1024]; 
     byte[] decoded; 
     int read = 0; 
     while ((read = imageStream.read(b)) != -1) { 
      // System.out.println(); 
      if (Base64.isArrayByteBase64(b)) { 
       decoded = Base64.decodeBase64(b); 
       out.write(decoded); 
      } 
     } 

     ret = true; 
    } catch (IOException e) { 
     StringWriter sw = new StringWriter(); 
     e.printStackTrace(new PrintWriter(sw)); 
     errorMessage = "error: " + sw; 

    } finally { 
     if (out != null) { 
      try { 
       out.close(); 
      } catch (Exception e) { 
       StringWriter sw = new StringWriter(); 
       e.printStackTrace(new PrintWriter(sw)); 
       System.out.println("Cannot close outputStream after writing file to disk!" + sw.toString()); 
      } 
     } 

    } 

    return ret; 
} 

/** 
* Helper method for the creation of a file output stream. 
* 
* @param imageFolder 
*   : folder where images are to be saved. 
* @param id 
*   : id of spcefic image file. 
* @return FileOutputStream object prepared to store images. 
* @throws FileNotFoundException 
*/ 
protected FileOutputStream createOutputStream(File imageFile) throws FileNotFoundException { 

    imageFile.getParentFile().mkdirs(); 

    return new FileOutputStream(imageFile); 
} 
相關問題