Java android将图像转换为byte []而不进行压缩,并将byte []转换为img

时间:2016-11-17 10:40:54

标签: java android arrays nullpointerexception android-bitmap

您好我想将Bitmap转换为byte []我这样做了:

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();

接下来我想将此byte []转换为Bitmap,我这样做了:

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

当我检查bmp.getWidth();时 并在Toast中显示:

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

我有nullpointer请帮帮我

1 个答案:

答案 0 :(得分:2)

copyPixelsToBuffer()的反面不是{​​{1}},而是decodeByteArray()

相关问题