Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android universal image loader clear cache

i use universal image loader to load image from url
this is adapter

public class BinderDataImg extends BaseAdapter {

static final String KEY_IMG = "img";
LayoutInflater inflater;
List<HashMap<String,String>> imgHashmap;
ViewHolder holder;

ImageLoader imageLoader = ImageLoader.getInstance();

public BinderDataImg(Activity act, List<HashMap<String,String>> map) {
    this.imgHashmap = map;
    inflater = (LayoutInflater) act
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

public int getCount() {
    // TODO Auto-generated method stub
    return imgHashmap.size();
}

public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null){
        vi = inflater.inflate(R.layout.list_img, null);
        holder = new ViewHolder();
        holder.iv_img =(ImageView)vi.findViewById(R.id.imageViewImg);

        vi.setTag(holder);
    }
    else{

        holder = (ViewHolder)vi.getTag();
    }

    String uri = imgHashmap .get(position).get(KEY_IMG);

    imageLoader.displayImage(uri, holder.iv_img);

    return vi;
}

static class ViewHolder{
    ImageView iv_img;
}

}

this is activity

List<ClassImg> imgList = null;
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisk(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .resetViewBeforeLoading(true)
            .displayer(new FadeInBitmapDisplayer(300))
            .showImageForEmptyUri(R.drawable.hinh_error)
            .showImageOnFail(R.drawable.hinh_internet)
            .showImageOnLoading(R.drawable.hinh_loading)
            .build();

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .threadPoolSize(3)
            .diskCacheSize(100 * 1024 * 1024)
            .build();

    ImageLoader.getInstance().init(config);

    final CustomListView lvi = (CustomListView)findViewById(R.id.listViewHinh);

    try {
        XmlPullParser parser = new XmlPullParser();
        imgList = parser.parse(getAssets().open(file_xml);
        BinderDataImg binderdata = new BinderDataImg(this, imgHashmap);
        lvi.setAdapter(binderdata);

    }catch(IOException e) {
        e.printStackTrace();
    }
}

I can load image and image cache in memory and disk is perfect.
but I want when activity start cache will clear or when press back button to leave activity will delete cache in memory and cache in disk.
how to do that?
sorry for my bad English
thank for reading.

like image 879
abcd1234 Avatar asked Mar 13 '23 11:03

abcd1234


1 Answers

You can use DiskCacheUtils and MemoryCacheUtils to remove specific image by image url

DiskCacheUtils.removeFromCache(imageUrl, ImageLoader.getInstance().getDiskCache());    
MemoryCacheUtils.removeFromCache(imageUrl, ImageLoader.getInstance().getMemoryCache());

or to completely clean cache

ImageLoader.getInstance().clearMemoryCache()
ImageLoader.getInstance().clearDiskCache()

To clear cache when activity created you can call this methods after you initialized your ImageLoader

To clear cache on leaving the activity you can call methods in onDestroy () , but i should mind that system can kill your activity without calling this method.

like image 82
Alex Avatar answered Mar 27 '23 18:03

Alex