如何在Kotlin Android中将base64字符串转换为图像

时间:2019-11-20 13:23:55

标签: android android-studio kotlin

我有一个Base64字符串,表示一个BitMap图像。

我需要再次将该String转换为BitMap图像,以便在我的Android应用中的ImageView上使用它

如何做到?

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码进行解码:-

val imageBytes = Base64.decode(base64String, Base64.DEFAULT)
val decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length)
imageView.setImageBitmap(decodedImage)

答案 1 :(得分:0)

您可以使用android方法

这里imageString是图像的base64String。

这是Java代码:

byte[] decodedByte = Base64.decode(imageString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedString.length); 

这是kotlin代码:

val decodedByte = Base64.decode(imageString, Base64.DEFAULT)
val bitmap = BitmapFactory.decodeByteArray(decodedByte, 0, decodedString.length) 

之后,您可以将其设置为图像视图

yourimage.setImageBitmap(bitmap);