base64中的编码图像返回图像

时间:2014-01-23 00:00:51

标签: java android image base64

所以我试图为我使用base64编码的图像取一个字符串,并将其转换回我可以在ImageView中使用的图像。编码它的代码是:

final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
image_str = Base64.encodeToString(b, Base64.DEFAULT);

我假设它会将image_str转换回byteArray然后返回位图?

我对base64函数不是很熟悉,所以我想在搜索的时候会问这里,以便在相同的时间内完成更多工作。

提前谢谢你,

泰勒

编辑:我确实找到了这段代码,但图片没有显示,logcat说解码返回false:

byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);

1 个答案:

答案 0 :(得分:2)

首先将Base64编码的字符串解码为字节:

byte[] decodedBytes = Base64.decode(image_str, Base64.DEFAULT);

然后将字节转换回JPG:

Bitmap bm = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
相关问题