android 如何在从 sqlite 数据库获取的 imageview 上显示位图
how to display a bitmap on imageview fetched from sqlite database in android
[我正在从我的 sqlite 数据库中检索 blob,其中 blob 图像的列索引为 9,10,11 并将它们存储为字节数组。稍后转换为位图并设置为图像视图。代码中没有错误。但是 imageview 没有显示图像。][1]
看这段代码:
您必须从光标加载图像字节并将其转换为位图。
byte[] imageBytes = getBlob(cursor, "ImageFieldName", null);
if (imageBytes != null)
{
Bitmap bmp= convertByteArrayToBitmap(imageBytes);
imageview1.setImageBitmap(bmp);
}
private byte[] getBlob(Cursor cursor, String colName, byte[] defaultValue) {
try {
int colIndex;
if (cursor != null && (colIndex = cursor.getColumnIndex(colName)) > -1
&& !cursor.isNull(colIndex))
return cursor.getBlob(colIndex);
return defaultValue;
} catch (Exception e) {
e.printStackTrace();
return defaultValue;
}
}
private Bitmap convertByteArrayToBitmap(byte[] bytes) {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}
[我正在从我的 sqlite 数据库中检索 blob,其中 blob 图像的列索引为 9,10,11 并将它们存储为字节数组。稍后转换为位图并设置为图像视图。代码中没有错误。但是 imageview 没有显示图像。][1]
看这段代码: 您必须从光标加载图像字节并将其转换为位图。
byte[] imageBytes = getBlob(cursor, "ImageFieldName", null);
if (imageBytes != null)
{
Bitmap bmp= convertByteArrayToBitmap(imageBytes);
imageview1.setImageBitmap(bmp);
}
private byte[] getBlob(Cursor cursor, String colName, byte[] defaultValue) {
try {
int colIndex;
if (cursor != null && (colIndex = cursor.getColumnIndex(colName)) > -1
&& !cursor.isNull(colIndex))
return cursor.getBlob(colIndex);
return defaultValue;
} catch (Exception e) {
e.printStackTrace();
return defaultValue;
}
}
private Bitmap convertByteArrayToBitmap(byte[] bytes) {
return BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
}