我想将 base64 编码的字符串转换为位图,这样我就可以把它放在 ImageView 中,但是会出现类似的错误
D/skia(7490): --- 解码器->解码返回假,位图返回空值
我的代码是:
byte[] imageAsBytes = Base64.decode(imageData);
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
最佳答案
首先您必须检查要解码的字符串是否有效并且具有要解码的预期值,为此,您可以执行以下操作:
filePath= Environment.getExternalStorageDirectory()
+ "/SaudiScore/temporary_holder.jpg";
Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String strBase64=Base64.encodeToString(byteArray, 0);
然后您可以解码刚刚编码的字符串并通过执行以下操作获取图像:
byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);
关于Android将base64编码的字符串转换为 ImageView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15683032/