我想將圖像存儲在SQLite DataBase
中。 我試圖存儲它使用BLOB
和String
,在這兩種情況下它存儲 圖像,可以檢索它,但是當我將它轉換爲Bitmap
使用 BitmapFactory.decodeByteArray(...)
它返回null。如何將字節數組轉換爲位圖
我已經使用這個代碼,但它返回null
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
我想將圖像存儲在SQLite DataBase
中。 我試圖存儲它使用BLOB
和String
,在這兩種情況下它存儲 圖像,可以檢索它,但是當我將它轉換爲Bitmap
使用 BitmapFactory.decodeByteArray(...)
它返回null。如何將字節數組轉換爲位圖
我已經使用這個代碼,但它返回null
Bitmap bitmap = BitmapFactory.decodeByteArray(blob, 0, blob.length);
就試試這個:
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* Ignored for PNGs */, blob);
byte[] bitmapdata = blob.toByteArray();
如果bitmapdata
是字節數組,然後讓Bitmap
像這樣做:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
返回解碼的Bitmap
或null
如果圖像無法解碼。
何時圖像不能解碼? –
如果您嘗試從其他格式的圖像解碼,則無法解碼圖像 – lxknvlk
如果我需要按順序執行多次這樣的操作,該怎麼辦?每次創建新的Bitmap對象是否耗費資源?我可以以某種方式將我的數組解碼到現有的位圖中嗎? –
Uttam的答案沒有爲我工作。我剛剛得到了空當我這樣做:
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);
在我的情況下,位圖數據只有像素的緩衝,所以它是IMPOSIBLE的功能decodeByteArray猜測其寬度,高度和顏色位數使用。所以,我想這和它的工作:
//Create bitmap with width, height, and 4 bytes color (RGBA)
Bitmap bmp = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(mBitmaps.get(minIndex).buffer);
bmp.copyPixelsFromBuffer(buffer);
檢查https://developer.android.com/reference/android/graphics/Bitmap.Config.html不同的顏色可供選擇
請閱讀此頁上的「相關」部分中的第5-10鏈接。 – Mat
在寫入數據庫之前,您是否對位圖進行了編碼? – Ronnie