字符串到位图在Kotlin
我是Kotlin的新手,我似乎无法做到这一点。 我得到一个base64String,我需要一个图像。
我做了:
val imageBytes = string.toByteArray(). // string is the base64image val image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
问题是,当我尝试访问image
,我得到一个SkAndroidCodec::NewFromStream returned null
在日志中SkAndroidCodec::NewFromStream returned null
消息。 我想在一个返回的方法中使用它,但它在return image
上一直崩溃。
我如何正确转换它?
我检查和字符串是不是空的,imageBytes有内容和imageBytes.size是60000以上。我使用相同的字符串在迅速和它转换图像没有任何修改,所以我相信,字符串不是问题。
val imageBytes = Base64.decode(string, 0) val image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size)
就这些。 你只需要将base 64字符串解码成一个字节数组。
用这个:
try { val imageBytes =Base64.decode(string,0); val image=BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.size); return image; } catch(Exception e) { e.getMessage(); return null; }