BitmapFactory 's decodeByteArray and decodeStream returns null
I am trying to decode ARGB byte
array into Bitmap
to display it as an ImageView
.
I tried using BitmapFactory
's decodeByteArray()
and decodeStream()
but both way gives null as Bitmap
.
But when I create bitmap using createBitmap()
and setPixels()
it works perfect. Here is the working code.
data is the byte array of size imageWidth*imageHeight*4
int[] pixels=new int[imageWidth*imageHeight];
int i=0,j=0;
while (i<imageWidth*imageHeight*4) {
pixels[j]= bytesToInt(data[i], data[i+1], data[i+2],data[i+3]);
i += 4;
j++;
}
Bitmap bitmap=Bitmap. createBitmap( imageWidth,imageHeight,Bitmap.Config .ARGB_8888)
bmpf.setPixels(pixels, 0, imageWidth , 0, 0, imageWidth, imageHeight);
//---------------------- definition of bytesToInt()
int bytesToInt(byte b1,byte b2,byte b3,byte b4开发者_运维技巧)
{
return (((b1& 0xff)<<24)+((b2 & 0xff)<<16)+((b3&0xff)<<8)+(b4&0xff));
}
But I need to achieve this using decodeByteArray()
or decodeStream()
because for the later way I need to create integer array for setPixels()
from the byte
array , which is inefficient.
What i am trying to implement is a video player , there fore it need to display around 15 frames/images per second.
Hopes some one can help me in this. Thanks in advance
Video plater plays video streams, i.e. a stream of video frames. Bitmap.decodeStream
expects a stream of image bytes, i.e. a stream of bytes in JPEG or PNG format.
Is your stream of bytes in correct format, i.e. jpeg or png?
精彩评论