0

我试图清除缓存onDestroy()事件如下。但它不起作用,缓存大小仍在增加。哪里错了?谢谢

 @Override
        protected void onDestroy() {
           super.onDestroy();
           try {
              trimCache(this);
           } catch (Exception e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
           }
        }

        public static void trimCache(Context context) {
           try {
              File dir = context.getCacheDir();
              if (dir != null && dir.isDirectory()) {
                 deleteDir(dir);
              }
           } catch (Exception e) {
              // TODO: handle exception
           }
        }

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

           // The directory is now empty so delete it
           return dir.delete();
        }
4

1 回答 1

0

尝试检查dir.list()的结果。可能某些目录没有内容,您在children.length处捕获NPE

于 2013-08-15T12:22:52.673 回答