如何使用通用图像装载机的Android强制清除缓存? [英] How to force a cache clearing using Universal Image Loader Android?
问题描述
我使用UIL在一个列表视图加载图像。
当我长preSS在ListView的形象,我展示一个对话框,修改图片,用新的使用相机取代它。
如果我拍一张新照片,当对话框被驳回我的列表视图仍显示旧形象(因为它是高速缓存)。如果我关闭并重新启动我的应用程序,当我去我的列表视图的新形象是正确那里。
这是我如何设置UIL:
//获取ImageLoader的的singletone实例
ImageLoader的= ImageLoader.getInstance();
//为图像加载器设置显示选项
DisplayImageOptions displayOptions =新DisplayImageOptions.Builder()
.cacheInMemory()
.displayer(新FadeInBitmapDisplayer(500))//淡入图像
.resetViewBeforeLoading()
。建立();
//设置图像加载器选项
ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(本).defaultDisplayImageOptions(displayOptions).build();
//初始化ImageLoader的与配置。
imageLoader.init(配置);
如果我删除 .cacheInMemory()
一切正常,虽然。我只是想知道,如果我只能打开我的对话框时,清除缓存。我想弄个所选的ImageView和呼叫 myImageView.invalidate()
没有成功打开的对话框时。
将图像从文件中加载:
//加载和显示图像异步
imageLoader.displayImage(_文件preFIX + image_path,图像);
有什么建议?
编辑:code创建一个上下文菜单时长pressing的形象,我试图清除缓存有:
@覆盖
公共无效onCreateContextMenu(文本菜单菜单,视图V,ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo);
//获取项目信息选择
AdapterView.AdapterContextMenuInfo信息;
尝试 {
//施放传入的数据对象转化为适配器视图对象的类型。
信息=(AdapterView.AdapterContextMenuInfo)menuInfo;
}赶上(ClassCastException异常E){
//如果菜单对象不能转换,记录一个错误。
Log.e(无信息,坏menuInfo,E);
返回;
}
光标光标=(光标)getListAdapter()的getItem(info.position)。
如果(光标== NULL){
//出于某种原因,请求项不可用,什么也不做
返回;
}
//从缓存中删除选定的图像(如果是图像)
IMAGEURL = cursor.getString(cursor.getColumnIndex(image_path));
如果(!imageUrl.equalsIgnoreCase()){
MemoryCacheUtil.removeFromCache(IMAGEURL,imageLoader.getMemoryCache());
}
Log.i(IMAGEURL,IMAGEURL);
//获取缺陷行ID和文本内容通过它投奔活动
defect_row_id = cursor.getLong(cursor.getColumnIndex(_ ID));
defect_txt = cursor.getString(cursor.getColumnIndex(瑕疵));
MenuInflater充气= getMenuInflater();
Log.i(光标,DatabaseUtils.dumpCursorToString(光标));
//在上下文菜单标题,值映射光标序列集项目识别
menu.setHeaderTitle(的getString(R.string.select_an_option));
inflater.inflate(R.menu.menu_defect_row,菜单);
}
在一个菜单项(编辑或删除)时
@覆盖
公共布尔onContextItemSelected(菜单项项){
// AdapterContextMenuInfo信息=(AdapterContextMenuInfo)item.getMenuInfo();
开关(item.getItemId()){
案例R.id.edit:
//使用指定的图像和缺陷pre-负载开路缺陷的活动
意图editDefectIntent =新的意图(这一点,DefectActivity.class);
editDefectIntent.putExtra(defect_row_id,defect_row_id);
editDefectIntent.putExtra(IMAGEURL,IMAGEURL);
startActivity(editDefectIntent);
返回true;
案例R.id.delete:
askDeleteConfirm();
返回true;
默认:
返回false;
}
} // onContextItemSelected
编辑:code显示图像列表
@覆盖
公共无效onResume(){
super.onResume();
//打开连接到数据库
DB =新DBAdapter(本);
db.open();
Log.i(DefectListActivity - > onResume,被称为);
//获取本机所有缺陷
defectList = db.getAllDefectsByUnit(unit_id);
//创建一个阵列适配器,让它来显示我们的排
缺陷=新SimpleCursorAdapter(这一点,R.layout.defect_row,defectList,新的String [] {缺陷,image_path},新的INT [] {R.id.defect,R.id.image},0);
使用ViewBinder //设置自定义视图
SimpleCursorAdapter.ViewBinder粘合剂=新SimpleCursorAdapter.ViewBinder(){
@覆盖
公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
INT placeholder_id = getResources()则getIdentifier(占位符,可拉伸,getPackageName())。
//获取列名
字符串名称= cursor.getColumnName(参数:columnIndex);
//为缩略图栏,如果我们有一个形象替换占位符
如果(image_path.equals(名称)){
ImageView的形象=(ImageView的)view.findViewById(R.id.image);
//位图的缩略图;
字符串image_path = cursor.getString(参数:columnIndex);
Log.i(image_path - >中,image_path);
如果(!image_path.equalsIgnoreCase()){
//加载和显示图像异步
imageLoader.displayImage(_文件preFIX + image_path,图像);
} 其他 {
image.setImageResource(placeholder_id);
}
返回true;
}
//对于缺陷列,只是文本添加到视图
如果(瑕疵.equals(名称)){
字符串defect_text = cursor.getString(参数:columnIndex);
TextView的defect_holder =(TextView中)view.findViewById(R.id.defect);
defect_holder.setText(defect_text);
返回true;
}
返回false;
}
};
defects.setViewBinder(粘合剂);
setListAdapter(缺陷);
} // onResume
如果您在内存盘缓存它既,例如:
ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCache(新UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.discCache(新UnlimitedDiscCache(cacheDir))
.........
确保你从他们两个人将其删除,然后重新装入您的图像视图。
MemoryCacheUtils.removeFromCache(URL,ImageLoader.getInstance()getMemoryCache());
DiscCacheUtils.removeFromCache(URL,ImageLoader.getInstance()getDiscCache());
I am using UIL to load images in a listview.
When I long press an image in the listview, I show a dialog to modify that picture, replacing it with a new one using the camera.
If I take a new picture, when the dialog is dismissed my listview still shows the old image (since it is cached). If I close and restart my application when I go to my listview the new image is correctly there.
This is how I set up UIL:
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
//set display options for image loader
DisplayImageOptions displayOptions = new DisplayImageOptions.Builder()
.cacheInMemory()
.displayer(new FadeInBitmapDisplayer(500)) //fade in images
.resetViewBeforeLoading()
.build();
//set image loader options
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).defaultDisplayImageOptions(displayOptions).build();
// Initialize ImageLoader with configuration.
imageLoader.init(config);
If I remove .cacheInMemory()
everything works though. I am just wondering if I can clear the cache when opening my dialog only. I tried to get hold of the ImageView selected and call myImageView.invalidate()
when opening the dialog without success.
The images are loaded from file:
// Load and display image asynchronously
imageLoader.displayImage(file_prefix + image_path, image);
Any suggestions?
Edit: code to create a context menu when long pressing an image, I tried to clear the cache there:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
//get info about item selected
AdapterView.AdapterContextMenuInfo info;
try {
// Casts the incoming data object into the type for AdapterView objects.
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
} catch (ClassCastException e) {
// If the menu object can't be cast, logs an error.
Log.e("no info", "bad menuInfo", e);
return;
}
Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
if (cursor == null) {
// For some reason the requested item isn't available, do nothing
return;
}
//remove selected image from cache (if it is an image)
imageUrl = cursor.getString(cursor.getColumnIndex("image_path"));
if (!imageUrl.equalsIgnoreCase("")) {
MemoryCacheUtil.removeFromCache(imageUrl, imageLoader.getMemoryCache());
}
Log.i("imageUrl", imageUrl);
//get defect row ID and text content to pass it to defect activity
defect_row_id = cursor.getLong(cursor.getColumnIndex("_id"));
defect_txt = cursor.getString(cursor.getColumnIndex("defect"));
MenuInflater inflater = getMenuInflater();
Log.i("cursor", DatabaseUtils.dumpCursorToString(cursor));
//set project identifier in context menu header, mapping cursor sequence of values
menu.setHeaderTitle(getString(R.string.select_an_option));
inflater.inflate(R.menu.menu_defect_row, menu);
}
When a menu item (edit or delete) is selected
@Override
public boolean onContextItemSelected(MenuItem item) {
//AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
//open defect activity with the specified image and defect pre-loaded
Intent editDefectIntent = new Intent(this, DefectActivity.class);
editDefectIntent.putExtra("defect_row_id", defect_row_id);
editDefectIntent.putExtra("imageUrl", imageUrl);
startActivity(editDefectIntent);
return true;
case R.id.delete:
askDeleteConfirm();
return true;
default:
return false;
}
}//onContextItemSelected
Edit: code to display list of images
@Override
public void onResume() {
super.onResume();
//open connection to db
db = new DBAdapter(this);
db.open();
Log.i("DefectListActivity -> onResume", "called");
// get all defects for this unit
defectList = db.getAllDefectsByUnit(unit_id);
// create an array adapter and let it to display our row
defects = new SimpleCursorAdapter(this, R.layout.defect_row, defectList, new String[] { "defect", "image_path" }, new int[] { R.id.defect, R.id.image }, 0);
//set custom view using ViewBinder
SimpleCursorAdapter.ViewBinder binder = new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int placeholder_id = getResources().getIdentifier("placeholder", "drawable", getPackageName());
//get column name
String name = cursor.getColumnName(columnIndex);
//for the thumbnail column,if we have an image replace the placeholder
if ("image_path".equals(name)) {
ImageView image = (ImageView) view.findViewById(R.id.image);
//Bitmap thumbnail;
String image_path = cursor.getString(columnIndex);
Log.i("image_path ->", image_path);
if (!image_path.equalsIgnoreCase("")) {
// Load and display image asynchronously
imageLoader.displayImage(file_prefix + image_path, image);
} else {
image.setImageResource(placeholder_id);
}
return true;
}
//for the defect column, just add the text to the view
if ("defect".equals(name)) {
String defect_text = cursor.getString(columnIndex);
TextView defect_holder = (TextView) view.findViewById(R.id.defect);
defect_holder.setText(defect_text);
return true;
}
return false;
}
};
defects.setViewBinder(binder);
setListAdapter(defects);
}//onResume
If you are caching it both in memory and disc, for example:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
.discCache(new UnlimitedDiscCache(cacheDir))
.........
Ensure you remove it from both of them, then reload your image view.
MemoryCacheUtils.removeFromCache(url, ImageLoader.getInstance().getMemoryCache());
DiscCacheUtils.removeFromCache(url, ImageLoader.getInstance().getDiscCache());
这篇关于如何使用通用图像装载机的Android强制清除缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!