0

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

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

请帮助我如何清除整个设备缓存,在这里我正在获取每个应用程序缓存位置,即缓存设备中所有应用程序的目录但是当我想删除它们时它返回 false

请帮助任何帮助都是可观的

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

提前致谢

4

1 回答 1

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

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

于 2013-03-05T10:28:02.167 回答