android - 在 sql 中显示存储为 varbinary 的 android 中的图像
问题描述
我在 SQL 表中存储为 varbinary 的图像,如何在 android 客户端中显示图像
解决方案
你需要做的是
- 将
Image
字符串从 DB 转换为ByteArray
- 在android中使用
BitmapFactory
转换ByteArray
为Bitmap
- 设置图像视图的位图
val imageText = "FFDD8FFE000104A4694600010101000000000000FFEE1138..."
myImageView.setImageBitmap(getBitmap(imageText))
您可以使用以下功能 [kotlin]
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)
}
推荐阅读
- django - get_absolute_url 创建空白链接
- node.js - 某些应用程序的 pm2 计划重新加载
- java - 使用 Java 流从员工列表中获取特定日期之前和之后的员工
- python-3.x - 从 many2many 向导(或)TransientModel 在 Many2one 字段中创建值
- php - PHP Curl 发布并获得响应
- c++ - 带有自定义重采样变换过滤器的 Directshow
- python - Python 中的函数将列表视为全局变量。如何解决这个问题?
- css - 在图像周围环绕背景填充
- react-native - 为什么在 React-Native 中使用 Drawer-Navigator 导航到另一个 Navigator 后出现空白屏幕?
- odoo - 在行中添加删除选项,就像我们在购物车中一样