简体   繁体   English

android通用图像加载器清除缓存

[英]android universal image loader clear cache

i use universal image loader to load image from url 我使用通用图像加载器从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. 谢谢你的阅读。

You can use DiskCacheUtils and MemoryCacheUtils to remove specific image by image url 您可以使用DiskCacheUtilsMemoryCacheUtils按图像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 要在创建活动时清除缓存,可以在初始化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. 要在离开活动时清除缓存,您可以在onDestroy ()调用方法,但我应该介意系统可以在不调用此方法的情况下终止您的活动。

ImageLoader class has two methods to clear caches: clearMemoryCache and clearDiscCache . ImageLoader类有两种清除缓存的方法: clearMemoryCacheclearDiscCache Call them in activity onDestroy method will do what you are looking for. 在活动中调用它们onDestroy方法将执行您要查找的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM