-6
[我從我的sqlite數據庫檢索blob圖像的列索引是9,10,11並將它們存儲爲字節數組blob。稍後轉換爲位圖並設置爲圖像視圖。代碼中沒有錯誤。但imageview不顯示圖像。 ][1]如何顯示位圖上的圖像取自sqlite數據庫android
[我從我的sqlite數據庫檢索blob圖像的列索引是9,10,11並將它們存儲爲字節數組blob。稍後轉換爲位圖並設置爲圖像視圖。代碼中沒有錯誤。但imageview不顯示圖像。 ][1]如何顯示位圖上的圖像取自sqlite數據庫android
看看這個代碼: 你必須從光標加載圖像字節並將其轉換爲位圖。
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);
}
謝謝你..它幫了我很多 – Soumya
是位圖真的檢索圖像,即位圖爲空? –