Volley Plus OutOfMemoryError in List Adapter

被刻印的时光 ゝ 提交于 2019-12-02 09:19:23

The problem is that you keep loading images but the memory they fill isn't released afterwards. You could simply if you are recycling a row test if the image is a bitmap and then call recycle() on it. You might use something like this:

if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.list_row_item_products, null);

        ViewHolder viewHolder = new ViewHolder();
        viewHolder.title = (TextView) rowView.findViewById(R.id.tvName);
        viewHolder.image = (ImageView) rowView.findViewById(R.id.list_image);

        rowView.setTag(viewHolder);
}
else {
    ViewHolder holder = (ViewHolder) rowView.getTag();
    if(holder.image.getDrawable() instanceof BitmapDrawable)
        ((BitmapDrawable) holder.image.getDrawable).getBitmap().recycle();
}

You should create SimpleImageLoader in onCreate() of Activity or some where else

    DiskLruBasedCache.ImageCacheParams cacheParams = new DiskLruBasedCache.ImageCacheParams(mContext, "CacheDirectory");
    cacheParams.setMemCacheSizePercent(0.5f);
    SimpleImageLoader mImageFetcher = new SimpleImageLoader(mContext, null, cacheParams);

If you are still facing OOM exception then you can use NetworkImageView.

Detailed example is present in github sample

Here is the solution for this Problem with changes in Adapter class. In which i placed

    DiskLruBasedCache.ImageCacheParams cacheParams = new DiskLruBasedCache.ImageCacheParams(mContext, "CacheDirectory");
    cacheParams.setMemCacheSizePercent(0.5f);
    SimpleImageLoader mImageFetcher = new SimpleImageLoader(mContext, null, cacheParams);

Inside if (rowView == null) which prevents to use above code repetitively in Adabpter Class.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!