【问题标题】:Android Clear External Image Cache On ExitAndroid 退出时清除外部图像缓存
【发布时间】:2012-08-31 19:29:09
【问题描述】:

如何执行 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();

    }

【问题讨论】:

标签: android caching


【解决方案1】:

试试这个。

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

【讨论】:

    【解决方案2】:

    这样的?

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

    【讨论】:

      【解决方案3】:

      我确定您已经实现了Lazy load of images in Android 的解决方案。

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

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

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

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

      【讨论】:

        【解决方案4】:

        您需要对 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();
          }
        }
        

        这样的东西应该可以工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-05
          • 2012-06-14
          • 1970-01-01
          • 2021-02-05
          • 2021-11-18
          • 2017-10-01
          • 2011-02-25
          • 2013-06-23
          相关资源
          最近更新 更多