缓存和临时文件/文件夹删除android

时间:2013-03-05 08:27:07

标签: android caching

嗨,朋友,谢谢以前的回复, 我在删除缓存和临时文件/文件夹方面遇到问题, 我需要的是从一个应用程序清理整个设备临时文件和缓存是我的应用程序 但在这里我只能清理我的应用程序缓存,这是我的代码

 private void mAppMethod(List<App> mApps) {
    // TODO Auto-generated method stub

            // File f = g
    for (int i = 0; i < mApps.size(); i++) {
          File dir = new  File("/data/data/"+mApps.get(i).getPackageName().concat("/cache"));

          Log.e("dir "+dir, "is directory "+dir.isDirectory());
            int j =    clearCacheFolder(dir, 10);
            if (dir!= null && dir.isDirectory())

                Log.e("j", "rff "+dir.delete());
            System.out.println(j+" rff "+dir.delete());

    }  

和我的清除缓存方法

 static int clearCacheFolder(final File dir, final int numDays) {

          int deletedFiles = 0;
          if (dir!= null && dir.isDirectory()) {
            //  System.out.println("here"+dir.delete());
              Log.e("here", "here  "+dir.isDirectory());
              try {

                  Log.e("here1", "here1"+dir.listFiles());
                  for (File child:dir.listFiles()) {
                      Log.e("here11", "here11");
                      //first delete subdirectories recursively
                      if (child.isDirectory()) {
                          Log.e("here111", "here111");
                          deletedFiles += clearCacheFolder(child, numDays);
                          Log.e("here1111", "here1111");
                      }
                      Log.e("here11111", "here11111");
                      //then delete the files and subdirectories in this dir
                      //only empty directories can be deleted, so subdirs have been done first
                      if (child.lastModified() < new Date().getTime() - numDays * DateUtils.DAY_IN_MILLIS) {
                           Log.e("here111111", "here111111");
                          if (child.delete()) {
                               Log.e("here1111111", "here1111111");
                              deletedFiles++;
                              Log.e("here11111111", "here11111111");
                          }
                      }
                  }
              }
              catch(Exception e) {
                  Log.e("TAG", String.format("Failed to clean the cache, error %s", e.getMessage()));
              }
          }
          return deletedFiles;
      }

请帮助我如何清除整个设备缓存,这里我得到每个应用程序缓存位置,即dir缓存设备中的所有应用程序,但当我想删除它们时返回false

请帮助任何有用的帮助

我能够清除一个应用程序的缓存,这是我运行此代码的应用程序,但不能用于其他应用程序

提前致谢

1 个答案:

答案 0 :(得分:0)

尝试使用此代码

public void clearApplicationData() {
    File cache = getCacheDir();
    File appDir = new File(cache.getParent());
    if(appDir.exists()){
        String[] children = appDir.list();
        for(String s : children){
            if(!s.equals("lib")){
                deleteDir(new File(appDir, s));
                Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s +" DELETED *******************");
            }
        }
    }
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    return dir.delete();
}

这将删除您的所有数据,并在应用程序中缓存文件。