-1

Hi I want to convert a Bitmap to byte[] i did this :

byte[] imageInByte;
Uri uri = data.getData();
bitmap = null;
try {
    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
} catch (IOException e) {
    e.printStackTrace();
}    
int size = bitmap.getRowBytes() * bitmap.getHeight();
ByteBuffer byteBuffer = ByteBuffer.allocate(size);
bitmap.copyPixelsToBuffer(byteBuffer);
imageInByte = byteBuffer.array();

Next I want to convert this byte[] to Bitmap and I did this :

Bitmap bmp = BitmapFactory.decodeByteArray(imageInByte, 0, imageInByte.length);

And when I what check a bmp.getWidth(); and show in Toast:

Toast.makeText(this,bmp.getWidth()+"",Toast.LENGTH_SHORT).show();

I have nullpointer please help me

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Krzysztof Pokrywka
  • 1,356
  • 4
  • 27
  • 50
  • @Community Wrong marking. The question was how to prevent a null bitmap. Not what a NullPointerException was. Please undo. – greenapps Nov 17 '16 at 17:08

1 Answers1

2

The reverse of copyPixelsToBuffer() is not decodeByteArray() but copyPixelsFromBuffer().

greenapps
  • 11,154
  • 2
  • 16
  • 19