[英]I can convert a byte array to Bitmap but the same process in reverse produces a null
所以我有一個字節數組,我可以使用下面的 function 將其轉換為 bitmap:
private Bitmap getBitMapFromByte(byte[] imageByte) {
Bitmap bitmap = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
return bitmap;
}
這會產生正確的結果,我可以在調試查看器中預覽返回的 bitmap。
然后我有一些中間函數,要求圖像位於字節數組byte[]
中。 所以我將 bitmap 轉換為byte[]
使用:
int byteCount = myBitmapImage.getAllocationByteCount();
ByteBuffer buffer = ByteBuffer.allocate(byteCount); //Create a new buffer
myBitmapImage.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] myByte = buffer.array();
現在相同的字節數組立即轉換回 bitmap 產生 null
Bitmap testBitmap= BitmapFactory.decodeByteArray(myByte, 0, myByte.length);
為什么字節數組立即轉換回 bitmap 返回 null?
要將 Bitmap 轉換為字節數組,請使用以下代碼
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
要將字節數組轉換為 bitmap 使用以下代碼
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, bitmapdata.length);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.