在android中显示图像,并在sql中存储为varbinary

在android中显示图像,并在sql中存储为varbinary,android,varbinary,Android,Varbinary,我在SQL表中将图像存储为varbinary,如何在android客户端中显示图像 您需要做的是 将图像字符串从DB转换为字节数组 在android中使用BitmapFactory将ByteArray转换为Bitmap 设置imageview的位图 val imageText=“ffdd8ffe000104a46946000101000000000000ffee1138…” myImageView.setImageBitmap(getBitmap(imageText)) 您可以使用以下

我在SQL表中将图像存储为varbinary,如何在android客户端中显示图像

您需要做的是
  • 图像
    字符串从DB转换为
    字节数组
  • 在android中使用
    BitmapFactory
    ByteArray
    转换为
    Bitmap
  • 设置imageview的位图
    • val imageText=“ffdd8ffe000104a46946000101000000000000ffee1138…”
    • myImageView.setImageBitmap(getBitmap(imageText))

您可以使用以下函数[kotlin]
我得到由以下原因引起的错误
:java.lang.IllegalArgumentException:bad base-64
请确保使用
FF…
而不是
0xFF…
,如果字符串有标题,请将其删除
图像
字段中存储的数据格式是什么?请提供一个完整的文本样本
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.google.android.gms.common.util.Hex

/**
 * Converts a hex string to Bitmap
 *
 * @param image  hex string e.g. "FFD8FFE0..."
 * @return Bitmap
 */
fun getBitmap(image: String): Bitmap {
    // Convert String to ByteArray
    val byteArray = Hex.stringToBytes(image)
    // Convert ByteArray Bitmap
    return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}