使用Bitmap.CompressFormat将位图转换为字节数组

时间:2013-06-10 11:55:30

标签: android bitmap bytearray

有没有办法在没有Android中的Bitmap.CompressFormat类的情况下将位图转换为字节数组?如果没有,怎么样?如果可能,请告诉我如何,thanx

1 个答案:

答案 0 :(得分:1)

这样的东西?

//b is the Bitmap

//calculate how many bytes our image consists of.
int bytes = b.getByteCount();
//or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
//int bytes = b.getWidth()*b.getHeight()*4; 

ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] array = buffer.array(); //Get the underlying array containing the data.

这; https://stackoverflow.com/a/10192092/1868384