如何在android中的sqlite数据库中获取的imageview上显示位图

时间:2016-10-01 09:38:08

标签: java android sqlite blob android-bitmap

[我正在从我的sqlite数据库中检索blob,其中blob图像的列索引是9,10,11并将它们存储为字节数组。稍后转换为位图并设置为imageview。代码中没有错误。但是imageview没有显示图像。][1]

1 个答案:

答案 0 :(得分: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);
    }