机房数据库BLOB数据类型解码问题

时间:2018-10-26 23:37:58

标签: android kotlin bitmap android-room

我正在使用以下代码将位图存储到具有ROOM持久层的数据库中:

val size = bitmap.byteCount
 val byteBuffer:ByteBuffer = ByteBuffer.allocate(size)
    val bytes = ByteArray(size)
    bitmap.copyPixelsToBuffer(byteBuffer)
    byteBuffer.rewind()
    byteBuffer.get(bytes)
    showDebugInfo(bytes.size.toString())
    Thread(Runnable {
       registered = true
       db.dataDao().insert(
       Customer(customerName.text.toString(),
       customerEmail.text.toString(),
       customerPhone.text.toString(),
       bytes
      )
    )
    }).start()

我的客户类别如下:

@Entity
class Customer(var name:String, var email:String, @PrimaryKey var phone:String, @ColumnInfo(typeAffinity = ColumnInfo.BLOB) var codeQR:ByteArray)

最后我对位图的解码如下:

val bitmap:Bitmap = BitmapFactory.decodeByteArray(customer!![0].codeQR,0,customer!![0].codeQR.size)
        codeQR.setImageBitmap(bitmap)

我遇到以下错误:

java.lang.IllegalStateException: BitmapFactory.decodeByte…ustomer!![0].codeQR.size) must not be null

0 个答案:

没有答案