2

I want to resize a Bitmap to store it in a HashMap for later use.

So I do the resizing in a AsyncTask.

final BitmapFactory.Options options = new BitmapFactory.Options();

@Override
protected Void doInBackground(byte[]... params) {
    byte[] bytes = params[0];
    options.inJustDecodeBounds = true;
    Bitmap test = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
    int imgHeight = test.getHeight();
    int imgWidth = test.getWidth();

    int counter = 0;
    while (imgHeight > 2000) {
        imgHeight = imgHeight / 2;
        counter++;
    }
    return null;
}

My problem is that test is null. the bytes is not null and has a length of 273440.

Where could be the problem?

Thank you for help!

Kind Regards!

raymondis
  • 356
  • 1
  • 5
  • 16

2 Answers2

2

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
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Thanks for the edit. That was my next question. Thank you for your explanation. I was wondering how the options gets the parameter. So I can delete `Bitmap test=` ? – raymondis Feb 10 '16 at 09:27
0

if u set to true then it will return null, as said in documentation

public boolean inJustDecodeBounds

Since: API Level 1 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.
JAAD
  • 12,349
  • 7
  • 36
  • 57