In my application I use an SQLite database to store the data of a lot of images. Some images can be up to 600x600 pixels. I use a custom list to create the bitmaps. I know there is a method bitmap.recycle(); but I'm not sure how to use that with a listview.
Asked
Active
Viewed 138 times
0
-
possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – tyczj Jul 21 '14 at 17:34
1 Answers
0
Here is the solution to handle bitmaps in android
First, you should calculate the sampleBitmapSize of the bitmap ( to load the lower version of the same bitmap )
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
Below is the util function to calculate InSampleSize ( an integer that defines the value (rating) of quality of a bitmap).
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
Log.i("ImageUtil", "InSampleSize: "+inSampleSize);
return inSampleSize;
}

Zeeshan Zulfiqar
- 91
- 1
- 8
-
using this technique i can handle 25kx25k pixel image easily on a non-ui thread. – Zeeshan Zulfiqar Jul 21 '14 at 17:36
-
Thank you, and in the reqWidth and reqHeight parameters I should put the needed sizes such as 600x600? – Timothy Logan Jul 21 '14 at 17:56
-
Yes, its the height and width of the bitmap that you want to have ( the smaller version of original bitmap). – Zeeshan Zulfiqar Jul 21 '14 at 18:04
-
One more question, would I be able to resize the image back to a very large image without distorting it? – Timothy Logan Jul 21 '14 at 18:15
-
You can create a byte array of the original image (see Bitmap to ByteArray Conversion). then you may pass to different functions ( make it constant to avoid any changes in data). – Zeeshan Zulfiqar Jul 21 '14 at 18:19