Android Novice
Android Novice

Reputation: 546

Android Clear External Image Cache On Exit

How do execute Clear(); in FileCache.class from another activity. I'm showing small parts of my codings. My objective is to clear the external cached file on every exit. Can anyone please show me how it is done. Thanks

FileCache.class

public class FileCache {

    private File cacheDir;

    public FileCache(Context context){
        //Find the dir to save cached images
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }

    public File getFile(String url){
        //I identify images by hashcode. Not a perfect solution, good for the demo.
        String filename=String.valueOf(url.hashCode());
        //Another possible solution (thanks to grantland)
        //String filename = URLEncoder.encode(url);
        File f = new File(cacheDir, filename);
        return f;

    }

    public void clear(){
        File[] files=cacheDir.listFiles();
        if(files==null)
            return;
        for(File f:files)
            f.delete();
    }

}

MyMainActivity.class

@Override
        public void onBackPressed() {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Do you want to exit?")
                   .setCancelable(false)
                   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                           Intent intent = new Intent(Intent.ACTION_MAIN);
                           intent.addCategory(Intent.CATEGORY_HOME);
                           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                           startActivity(intent);
                           new clear();  //<<--- How do i Call clear(); in FileCache.class
                           System.exit(0);
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                       }
                   });
            AlertDialog alert = builder.create();
            alert.show();

    }

Upvotes: 2

Views: 1419

Answers (4)

Kuitsi
Kuitsi

Reputation: 1675

Something like this?

public void onClick(DialogInterface dialog, int id) {
    new  FileCache( MyMainActivity.this ).clear();
}

Upvotes: 1

stealthjong
stealthjong

Reputation: 11093

You need a reference to the FileCache object. I suppose you create it in the onCreate() from the activity. If so, make the FileCache an attribute from the activity. That way, you can call myFileCacheReference.clear() from the onBackPressed().

public class MainActivity extends Activity {
  private FileCache myFileCacheRef;

  public void onCreate(Bundle b) {
    //standard stuff
    myFileCacheRef = new FileCache();
  }

  public void onBackPressed() {
    myFileCacheRef.clear();
  }
}

something like this should work.

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

I am sure you have implemented the solution for Lazy load of images in Android.

There is already clearCache() method given in ImageLoader class:

 public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }

So you can clear the cache by calling clearCache() method as below:

ImageLoader imgLoader = new ImageLoader(mContext);
imgLoader.clearCache();

Upvotes: 0

KC Chai
KC Chai

Reputation: 1617

Try this.

public void onClick(DialogInterface dialog, int id) {

                               Intent intent = new Intent(Intent.ACTION_MAIN);
                               intent.addCategory(Intent.CATEGORY_HOME);
                               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                               startActivity(intent);
                               FileCache loader = new FileCache(null);
                               loader.clear();  
                               System.exit(0);
                           }

Upvotes: 2

Related Questions