【问题标题】:Java android convert image to byte [] without compress and convert byte[] to img [duplicate]Java android将图像转换为字节[]而不压缩并将字节[]转换为img [重复]
【发布时间】:2016-11-17 10:40:53
【问题描述】:

嗨,我想将位图转换为字节 [] 我这样做了:

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

接下来我想将此字节 [] 转换为位图,我这样做了:

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

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

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

我有空指针,请帮帮我

【问题讨论】:

  • @Community 标记错误。问题是如何防止空位图。不是 NullPointerException 是什么。请撤消。

标签: java android arrays nullpointerexception android-bitmap


【解决方案1】:

copyPixelsToBuffer() 的反面不是decodeByteArray() 而是copyPixelsFromBuffer()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    相关资源
    最近更新 更多