android universal image loader clear cache

让人想犯罪 __ 提交于 2019-12-23 13:22:55

问题


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.


回答1:


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.




回答2:


ImageLoader class has two methods to clear caches: clearMemoryCache and clearDiscCache. Call them in activity onDestroy method will do what you are looking for.



来源:https://stackoverflow.com/questions/36910809/android-universal-image-loader-clear-cache

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