2016-02-28 85 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

相关问题