2016-02-28 87 views
0

我以下列方式編碼的圖像,並將其存儲在我的數據庫:位圖 - Base64編碼字符串 - 位圖轉換的Android

public String getStringImage(Bitmap bmp){ 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] imageBytes = baos.toByteArray(); 
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT); 
    return encodedImage; 
} 

現在,我想它以下列方式進行解碼,並在顯示它ImageView

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

} 

然而ImageView保持空白,並且不顯示圖像。我錯過了什麼嗎?

回答

2

嘗試先從Base64解碼字符串。

public static Bitmap decodeBase64(String input) { 
     byte[] decodedByte = Base64.decode(input, 0); 
     return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
} 

在你的情況:

try{ 
     byte[] decodedByte = Base64.decode(input, 0); 
     InputStream stream = new ByteArrayInputStream(decodedByte); 
     Bitmap bitmap = BitmapFactory.decodeStream(stream); 
     return bitmap; 
    } 
    catch (Exception e) { 
     return null; 
    } 
+0

你是說我應該這樣做,而不是我的try/catch塊或補充呢? – Alk

+0

它工作時,我只是用你提供的代碼替換try catch) – Alk

相關問題