You are wasting memory becuase you've set inJustDecodeBounds after you decoded the whole image once. (I guess you get OOM because of that later on). Consider closing your inputstream as well.
BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
options.inJustDecodeBounds = true;
options.inSampleSize = calculateInSampleSize(options, 1024, 128);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,options);
return bitmap;
I think you really meant to do:
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
options.inSampleSize = calculateInSampleSize(options, 1024, 128);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length,options);
return bitmap;
That will get the width and height of the image without storing the bitmap (just get options) and then you subsample the image to correct size after that and returns the bitmap.
I don't understand why you resize the bitmap again in onPostExexute
of your asynctask, imageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, bitmap.getHeight()/2, bitmap.getWidth(), bitmap.getHeight()/2));
When you just completed to load the bitmap, why not load the correct size to begin with? (after doing the above changes i would suggest you to log the sizes and see if you really need to resize again in onPostExexute
)
Have you looked at image loading libraries?
There's quite a few available, one that seems to fit your needs is Picasso
Where one can do such things as: Picasso.with(context).load("url_to_image").fit().into(imageView);
This downloads and caches the bitmap (in a background thread) and fits and draws into an imageView, all in one line, neat!