How to reduce memory consumption in android
Possible Duplicate:
Android: Strange out of memory issue while loading an image to a Bitmap object
I am new in android field. I don't known the how to reduce the memory consumption in android. In my application large number of image is drawn from the web and display into grid view. When running the application "out memory problem is occur".
Please help me
1) scale down and reduce the size of images
/**
* decodes image and scales it to reduce memory consumption
*
* @param file
* @param requiredSize
* @return
*/
public static Bitmap decodeFile(File file, int requiredSize) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(file), null, o);
// The new size we want to scale to
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < requiredSize
|| height_tmp / 2 < requiredSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file),
null, o2);
return bmp;
} catch (FileNotFoundException e) {
} finally {
}
return null;
}
2) use bitmap.Recycle();
3) use System.gc();
for indicating to the VM that it would be a good time to run the garbage collector
下一篇: 如何减少android的内存消耗