如何删除应用程序的缓存文件夹? [英] How to delete cache-folder of app?

查看:111
本文介绍了如何删除应用程序的缓存文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过缓存的Andr​​oid的文件读取的(见的数据存储文档的,但我没有得到我怎么能清洁整个文件夹。

那么,如何才能删除我的应用程序的缓存文件夹?它在这条道路:

  

/Android/data/de.stepforward/cache /

解决方案

在放这的onDestroy code()来清除应用程序缓存:

 无效的onDestroy(){super.onDestroy();

    尝试 {
        trimCache(本);
       // Toast.makeText(这一点,的onDestroy,Toast.LENGTH_LONG).show();
    }赶上(例外五){
        // TODO自动生成的catch块
        e.printStackTrace();
    }

}

公共静态无效trimCache(上下文的背景下){
    尝试 {
       文件DIR = context.getCacheDir();
       如果(DIR = NULL和放大器;!&安培; dir.isDirectory()){
          deleteDir(DIR);
       }
    }赶上(例外五){
       // TODO:处理异常
    }
 }

 公共静态布尔deleteDir(文件目录){
    如果(DIR = NULL和放大器;!&安培; dir.isDirectory()){
       的String []孩子= dir.list();
       的for(int i = 0; I< children.length;我++){
          布尔成功= deleteDir(新建文件(目录,孩子们[I]));
          如果(!成功){
             返回false;
          }
       }
    }

    //该目录现在是空的,所以它删除
    返回dir.delete();
 }
 

I read through the Android documentation of the cache (see Data Storage Documentation) but I didn't got how I can clean the whole folder.

So how can I delete the cache-folder of my app? It's in this path:

/Android/data/de.stepforward/cache/

解决方案

Put this code in onDestroy() to clear app cache:

void onDestroy() { super.onDestroy();

    try {
        trimCache(this);
       // Toast.makeText(this,"onDestroy " ,Toast.LENGTH_LONG).show();
    } 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();
 }

这篇关于如何删除应用程序的缓存文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆