在adapter-Android中显示相同的图像文件
情况: 我有一个活动,它包含一个用于选择图像的图像选择器函数。选择后,这些图像将加载/显示在在adapter-Android中显示相同的图像文件,android,android-adapter,android-gridview,android-file,Android,Android Adapter,Android Gridview,Android File,情况: 我有一个活动,它包含一个用于选择图像的图像选择器函数。选择后,这些图像将加载/显示在GridView中。我使用ImageLoader库通过以下方法加载图像: ImageLoader.getInstance().displayImage(“文件:/”+image.path,实用程序.displayImageOptions) 在同一个活动中,我有一个预览按钮,单击该按钮可引导另一个新活动。此新活动还包含一个GridView,使用与上一活动相同的适配器代码。但是,此GridView包含先前选择
GridView
中。我使用ImageLoader
库通过以下方法加载图像:
ImageLoader.getInstance().displayImage(“文件:/”+image.path,实用程序.displayImageOptions)代码>
在同一个活动中,我有一个预览按钮,单击该按钮可引导另一个新活动。此新活动还包含一个GridView
,使用与上一活动相同的适配器代码。但是,此GridView
包含先前选择的图像的压缩版本
在压缩过程中,我保存这些位图并创建新文件,这些文件将在第二个活动中加载
saveBitmapToFile(bitmap, index);
private void saveBitmapToFile(Bitmap bitmap, int imageIndex){
try {
bitmapFile = getPermanentFile(imageIndex);
FileOutputStream fos = new FileOutputStream(bitmapFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Images image = new Images(bitmapFile.getAbsolutePath(), true, false);
compressedImageList.add(image);
}
catch (IOException e){
e.printStackTrace();
}
}
private static File getPermanentFile(int imageIndex) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE + String.valueOf(imageIndex) + ".jpg");
try {
if (file.exists()){
Log.v("File exists", file.getName());
file.delete();
file = new File(Environment.getExternalStorageDirectory(), TEMP_PHOTO_FILE + String.valueOf(imageIndex) + ".jpg");
}
file.createNewFile();
} catch (IOException e) {
}
return file;
} else {
return null;
}
}
我遇到的问题是,假设我开始使用一个新的应用程序(缓存中没有存储任何内容),选择4个图像,然后单击预览,我会得到这4个图像的压缩版本。但随后我关闭应用程序并再次启动它,然后我再次选择4张不同的图像并点击预览,但我得到了与第一次相同的4张压缩图像。通过进入设置清除缓存/数据可以解决问题,但如何在代码中执行此操作
我删除了这些文件onBackPressed()
,但当我单击“上一步”,然后再次选择“图像”时,问题仍然存在
@Override
public void onBackPressed(){
super.onBackPressed();
for (Images file : compressedImageList){
File f = new File(file.cardPath);
boolean deleted = f.delete();
if (deleted){
Log.v("File deleted : ", file.cardPath);
}
}
compressedImageList.clear();
}
你试过了吗
imageLoader.clearMemoryCache();
及
在选择新图像之前,请删除sd卡上的文件。@bharat是的,请查看更新的帖子。如果您在背面按下复选框,文件将从sd卡上删除。应用程序关闭时检查sd卡一次删除文件
imageLoader.clearDiscCache();