I want to load a big size (about 10 MB) image and let user add some effects (such as Grayscale and so on). So, I loaded the bitmap using inSampleSize
to show a preview to user:
public Bitmap load(Context context, int[] holderDimention, String image_url) throws Exception {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image_url, bitmapOptions);
int inSampleSize = 1;
final int outWidth = bitmapOptions.outWidth;
final int outHeight = bitmapOptions.outHeight;
final int holderWidth = holderDimention[0];
final int holderHeight = holderDimention[1];
// Calculation inSampleSize
if (outHeight > holderHeight || outWidth > holderWidth) {
final int halfWidth = outWidth / 2;
final int halfHeight = outHeight / 2;
while ((halfHeight / inSampleSize) > holderHeight && (halfWidth / inSampleSize) > holderWidth) {
inSampleSize *= 2;
}
}
bitmapOptions.inSampleSize = inSampleSize;
// Decoding bitmap
bitmapOptions.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(image_url, bitmapOptions);
}
Finally,in background I want to apply effects on the original image without downsampling (it is necessary). So I used the following code inside a subclass of AsyncTask
:
public Bitmap load(Context context, String image_url) throws IOException {
return BitmapFactory.decodeFile(image_url);
}
But I got an out of memory exception. How could I handle this problem?