简体   繁体   中英

how to display a bitmap on imageview fetched from sqlite database in android

[I am retrieving blob from my sqlite database where column index of the blob images are 9,10,11 and storing them as byte array. Later converting to bitmap and set to an imageview. There is no error in the code. but imageview is not displaying the image. ][1]

look at this code: you must load image bytes from cursor and convert it to Bitmap.

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM