如何删除应用程序的缓存文件夹?

编程入门 行业动态 更新时间:2024-10-27 19:19:04
本文介绍了如何删除应用程序的缓存文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我通过缓存的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(); }

更多推荐

如何删除应用程序的缓存文件夹?

本文发布于:2023-11-12 17:05:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:缓存   应用程序   文件夹

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!