在适配器中显示相同的图像文件 - Android

Displaying the same image files in adapter - Android

情况:

我有一个 activity,其中包含 select 图片的图片选择器功能。一旦 select 编辑,这些图像就会 loaded/displayed 在 GridView 中。我使用 ImageLoader 库通过以下方法加载图像:

ImageLoader.getInstance().displayImage("file://"+image.path, Utility.displayImageOptions);

在同一个 activity 中,我有一个 预览按钮 ,单击它会导致另一个新的 activity。这个新 activity 还包含一个 GridView,使用与之前 activity 相同的适配器代码。但是,此 GridView 包含之前 select 编辑的图像的压缩版本。

在我的压缩中,我保存了这些位图并创建了在第二个 activity 中加载的新文件。

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;
        }
    }

我遇到的问题是说我从一个新的应用程序开始(缓存中没有存储任何内容),select 4 张图像并单击 Preview,我得到了压缩这四个图像的版本。但是然后我关闭应用程序并再次启动它,然后我再次 select 4 个不同的图像 并点击预览但我得到了我第一次得到的相同的 4 个压缩图像。通过进入设置清除 cache/data 可以解决问题,但我如何在代码中执行此操作。

我删除了这些文件 onBackPressed() 但是当我点击返回然后再次点击 select 图片时问题仍然存在。

@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();

imageLoader.clearDiscCache();