When i try to an image decode with BitmapFactory it cause GC_FOR_ALLOC all the time, and then the application crashed OutOfMemory.
Any suggestion for fix it? I need to solve this problem for a GridView with a lot of images (381 images 90k each one).
My code from AsyncTask:
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
final Bitmap bitmap = decodeSampledBitmapFromResource(
context.getResources(), data, 100, 100);
addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
return bitmap;
}
And my decodeSampledBitmapFromResource method:
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);
}
If they are the same size you can resize into an unused old bitmap (you'll need to keep a referance counter so you can know when it's no longer in use by the gridview).
See https://developer.android.com/training/displaying-bitmaps/manage-memory.html
Also if you run out of memory with the code you have now your memory cache is likely too big... Use a heap dump or something like it to find out whats leaking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.