Android在退出时清除外部图像缓存



如何执行Clear();在另一个活动的FileCache.class中。我正在展示我编码的小部分。我的目标是在每次退出时清除外部缓存文件。谁能告诉我它是怎么做的吗。感谢

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

试试这个。

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

类似的东西?

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

我相信您已经在Android中实现了图像懒惰加载的解决方案。

ImageLoader类中已经给出了clearCache()方法:

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

因此,您可以通过调用clearCache()方法来清除缓存,如下所示:

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

您需要对FileCache对象的引用。我假设您在活动的onCreate()中创建它。如果是,请使FileCache成为活动中的一个属性。这样,您就可以从onBackPressed()调用myFileCacheReference.clear()

public class MainActivity extends Activity {
  private FileCache myFileCacheRef;
  public void onCreate(Bundle b) {
    //standard stuff
    myFileCacheRef = new FileCache();
  }
  public void onBackPressed() {
    myFileCacheRef.clear();
  }
}

这样的东西应该行得通。

最新更新