Why Does Bitmapfactory.Decodebytearray Return Null

Why does BitmapFactory.decodeByteArray return null?

From the documentation:

Returns The decoded bitmap, or null if the image could not be decode.

The bytes involved in the string "test" aren't a valid bitmap, are they?

If you saved the text "test" in a file called foo.png or foo.jpg etc and tried to open it in Windows, what would you expect the result to be? It would be an error: those bytes simply aren't a valid image in any known format.

EDIT: I don't know anything about Android graphics, but your update certainly looks like a much more reasonable way to draw text onto a bitmap.

Why is my BitmapFactory.decodeByteArray returning null?

Turned out to be a database issue. SQLite gives you a cursor of 1MB MAX size. I was getting the bytes from database, with a 1MB cursor, the pic was not sent properly.

To fix it, I stored the path to the photo in database instead of the bytes.

BitmapFactory.decodeByteArray returns null with inJustDecoteBounds=true in Android

My problem is that test is null.

that's the expected behaviour. From the documentation

if set to true, the decoder will return null (no bitmap), but the
out... fields will still be set, allowing the caller to query the
bitmap without having to allocate the memory for its pixels.

You have to use options, to retrieve the bitmap's meta-info you want. E.g.

BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
int imgWidth = options.outWidth

BitmapFactory.decodeByteArray() always returns null (manually-created byte array)

decodeByteArray of BitmapFactory actually decodes an image, i.e. an image that has been encoded in a format such as JPEG or PNG. decodeFile and decodeStream make a little more sense, since your encoded image would probably be coming from a file or server or something.

You don't want to decode anything. You are trying to get raw image data into a bitmap. Looking at your code it appears you are generating a 225 x 160 bitmap with 4 bytes per pixel, formatted ARGB. So this code should work for you:

    int width = 225;
int height = 160;
int size = width * height;
int[] pixelData = new int[size];
for (int i = 0; i < size; i++) {
// pack 4 bytes into int for ARGB_8888
pixelData[i] = ((0xFF & (byte)255) << 24) // alpha, 8 bits
| ((0xFF & (byte)255) << 16) // red, 8 bits
| ((0xFF & (byte)0) << 8) // green, 8 bits
| (0xFF & (byte)0); // blue, 8 bits
}

Bitmap image = Bitmap.createBitmap(pixelData, width, height, Bitmap.Config.ARGB_8888);

BitmapFactory.decodeByteArray returns null when decoding data from ByteBuffer.array()

It seems that if I first read the data from the ByteBuffer into an array and then provide this data,BitmapFactory.decodeByteArray is able to decode the image correctly. I missed the offset inside the backing array where the ByteBuffer data actually starts.

So the correct code would be:

Bitmap im =  BitmapFactory.decodeByteArray(inputData.array(), inputData.arrayOffset(), inputData.limit());

Why BitmapFactory.decodeByteArray() returns null?

You can try adding Base64.NO_WRAP:

byte[] decodeResponse = Base64.decode(base64Image, Base64.DEFAULT | Base64.NO_WRAP);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodeResponse, 0, decodeResponse.length);

Hope this helps someone.



Related Topics



Leave a reply



Submit