Displaying the same image files in adapter - Android

132 views Asked by At

Situation:

I have an activity that contains an image picker function to select images. Once selected, these images are loaded/displayed in a GridView. I load the images using ImageLoader library with the following method:

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

In the same activity i have a Preview button which clicked lead to an another new activity. This new activity also contains a GridView using the same adapter code as in the previous activity. However, this GridView contains the compressed version of the images that were selected previously.

In my compression i save these bitmaps and create new files that are loaded in the second 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;
        }
    }

The problem i have is that say I start with a fresh app (nothing stored in cache), select 4 images and click Preview, i get the compressed version of these four images. But then i close the app and start it again and i again select 4 different images and hit Preview BUT i get the same 4 compressed images that I got at the first time. Clearing the cache/data by going in settings resolves the problem but how can i do this in code.

I delete these files onBackPressed() but still the problem remains when i click back and then select images again.

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

There are 1 answers

0
Lena Bru On BEST ANSWER

Have You tried

imageLoader.clearMemoryCache();

and

imageLoader.clearDiscCache();