Android Studio,科特林:BitmapFactory.decodeStream() returns 空
Android Studio, Kotlin: BitmapFactory.decodeStream() returns null
我正在从我的数据库中获取一个表示图像的字符串。我想再次将它转换成图像,所以首先我将它制成一个字节数组,然后尝试对其进行解码。发生的事情是,由于某种原因,它 returns null 不应该是这种情况。我打印了数组的大小,看看转换是否有错误,但那里没有任何问题。
private fun imageBit(image: String): Bitmap{
val b = image.toByteArray()
d("DEBUG2", "${b.size}") //prints 47109
val ins = ByteArrayInputStream(b)
d("DEBUG3", "${ByteArrayInputStream(b).readBytes().size}") //prints 47109
return BitmapFactory.decodeStream(ins)
}
试试这个:
private fun imgBit(image: String): Bitmap{
val imgBytes = Base64.decode(image, 0)
return BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.size)
}
我正在从我的数据库中获取一个表示图像的字符串。我想再次将它转换成图像,所以首先我将它制成一个字节数组,然后尝试对其进行解码。发生的事情是,由于某种原因,它 returns null 不应该是这种情况。我打印了数组的大小,看看转换是否有错误,但那里没有任何问题。
private fun imageBit(image: String): Bitmap{
val b = image.toByteArray()
d("DEBUG2", "${b.size}") //prints 47109
val ins = ByteArrayInputStream(b)
d("DEBUG3", "${ByteArrayInputStream(b).readBytes().size}") //prints 47109
return BitmapFactory.decodeStream(ins)
}
试试这个:
private fun imgBit(image: String): Bitmap{
val imgBytes = Base64.decode(image, 0)
return BitmapFactory.decodeByteArray(imgBytes, 0, imgBytes.size)
}