program tip

바이트 배열을 비트 맵으로 변환하는 방법

radiobox 2020. 8. 3. 08:35
반응형

바이트 배열을 비트 맵으로 변환하는 방법


에 이미지를 저장하고 싶습니다 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이미지를 디코딩 할 수없는 경우를 반환합니다 .


Uttam의 대답은 저에게 효과적이지 않았습니다. 내가 할 때 방금 null을 얻었습니다.

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

필자의 경우 비트 맵 데이터에는 픽셀 버퍼 만 있기 때문에 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(bitmapdata);
bmp.copyPixelsFromBuffer(buffer);

다른 색상 옵션은 https://developer.android.com/reference/android/graphics/Bitmap.Config.html확인 하십시오.

참고 URL : https://stackoverflow.com/questions/7620401/how-to-convert-byte-array-to-bitmap

반응형