android {//build.gradle文件 useLibrary "org.apache.http.legacy" }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); memorySize = (TextView)findViewById(R.id.memorySize); //获取缓存大小 long size = FileUtil.getFolderSize(FileUtil.getRootCacheDir(getApplicationContext())); double a = size/1024; if(a < 1024){ memorySize.setText(a + "KB"); }else{ a = size/1024/1024; memorySize.setText(a + "M"); } }
/** * 点击事件调用清除缓存方法 */ private void clearMemory(){ FileUtil.deleteFolderFile(FileUtil.getRootCacheDir(getApplicationContext()).getPath(),true); memorySize.setText("0KB"); }
//工具类 public abstract class FileUtil { private static final String TAG = "FileUtil"; //图片缓存的路径 public static final String CACHE_DIR_IMAGE = "images"; //本地数据库保存的路径 public static final String CACHE_DIR_DATABASE = "database"; //语音录制保存的路径 public static final String CACHE_DIR_VOICE = "voice"; //文件下载的路径 public static final String CACHE_DIR_DOWNLOAD = "download"; //解压的html文件保存路径 public static final String CACHE_DIR_HTML = "html"; //AQUtility框架存储目录 public static final String CACHE_DIR_AQUERY = "aquery"; //crash奔溃日志保存路径 public static final String CACHE_DIR_CRASH = "crash"; //apk版本更新保存目录 public static final String CACHE_DIR_APK = "apk"; //webView缓存目录 public static final String CACHE_DIR_WEBVIEW = "webview"; /** * 判断当前是否有SD卡 * * @return 存在返回True, 不存在返回False */ public static boolean isSDCardExist() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return true; } return false; } /** * 获取应用缓存根目录 * * @param context * @return * @throws Exception */ public static File getRootCacheDir(Context context) { File dir = null; if (isSDCardExist()) { dir = context.getExternalCacheDir(); if(dir == null){ dir = context.getCacheDir(); } } else { dir = context.getCacheDir(); } return dir; } /** * 获取指定文件的的缓存目录 * * @param context * @return */ private static File getCacheDir(Context context, String dirName) { StringBuilder sb = new StringBuilder(getRootCacheDir(context).getPath()).append(File.separator).append(dirName).append(File.separator); File file = new File(sb.toString()); if (!file.exists()) { file.mkdirs(); } return file; } /** * 图片缓存目录 * * @param context * @return */ public static File getImageCacheDir(Context context) { return getCacheDir(context, CACHE_DIR_IMAGE); } /** * 返回数据库缓存目录 * * @param context * @return */ public static File getDatabaseCacheDir(Context context) { return getCacheDir(context, CACHE_DIR_DATABASE); } /** * 录取语音的缓存目录 * * @param context * @return */ public static File getVoiceCacheDir(Context context) { return getCacheDir(context, CACHE_DIR_VOICE); } /** * 获取文件下载目录 * @param context * @return */ public static File getDownoadCacheDir(Context context){ return getCacheDir(context,CACHE_DIR_DOWNLOAD); } /** * 获取html解压路径 * @param context * @return */ public static File getHtmlCacheDir(Context context){ return getCacheDir(context,CACHE_DIR_HTML); } /** * AQUtility框架存储目录 * @param context * @return */ public static File getAqueryCacheDir(Context context){ return getCacheDir(context,CACHE_DIR_AQUERY); } /** * crash奔溃日志保存路径 * @param context * @return */ public static File getCrashCacheDir(Context context){ return getCacheDir(context,CACHE_DIR_CRASH); } /** * apk版本更新保存目录 * @param context * @return */ public static File getApkCacheDir(Context context){ return getCacheDir(context,CACHE_DIR_APK); } /** * 获取webView的缓存目录 * @param context * @return */ public static File getWebViewCacheDir(Context context){ return getCacheDir(context,CACHE_DIR_WEBVIEW); } /** * 删除文件夹 * * @param path * @param delSub 如果文件夹非空,是否删除其下属的子文件夹、文件? * @throws IOException */ public static void deleteFolderFile(String path, boolean delSub) { if (TextUtils.isEmpty(path)) { return; } File file = new File(path); if (!file.isDirectory()) { file.delete(); } else { File[] afile = file.listFiles(); if (afile.length == 0) { file.delete(); return; } // 对于目录:是否循环删除所有子目录及其文件? if (delSub) { for (int i = 0; i < afile.length; i++) { if (afile[i].isDirectory()) { deleteFolderFile(afile[i].getAbsolutePath(), true); }else { afile[i].delete(); } } // 删除空目录 if (file.listFiles().length == 0) { file.delete(); } } } } /*** * 根据文件路径删除单个文件 * * @param path */ public static void deleteSingleFile(String path) { if (TextUtils.isEmpty(path)) { return; } File file = new File(path); file.delete(); } /** * 返回文件夹下的文件总尺寸 * * @param file * @return * @throws Exception */ public static long getFolderSize(File file){ long total = 0L; File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) total += getFolderSize(files[i]); else total += files[i].length(); } return total; } /** * 读取文件内容 * * @param file * @return * @throws IOException */ public static String readFile(File file) throws IOException { if (file == null) { throw new IOException(); } else { String s = new String(toByteArray(new FileInputStream(file))); Log.d(TAG, s); return s; } } /** * 读取文件内容 * * @param in * @return * @throws IOException */ private static byte[] toByteArray(InputStream in) throws IOException { ByteArrayBuffer bos = new ByteArrayBuffer(4096); byte abyte0[] = new byte[4096]; try { int len; while ((len = in.read(abyte0)) != -1) { bos.append(abyte0, 0, len); } return bos.toByteArray(); } finally { in.close(); } } /** * 添加文件内容 * * @param data 新添加的内容 * @param filename 文件名称 * @throws IOException */ public static void writeFile(Context context,String dirName, String data, String filename) throws IOException { if (TextUtils.isEmpty(data)) data = ""; if (TextUtils.isEmpty(filename)) throw new IOException(); File dir = getCacheDir(context,dirName); File f = new File(dir, filename); if (!f.exists()) f.createNewFile(); FileOutputStream fos = null; try { fos = new FileOutputStream(f, true); fos.write(data.getBytes()); fos.flush(); } finally { try { fos.close(); } catch (Exception ex) { } } } /** * 解压缩文件 * * @param zipFile * @param outPath */ public static void unZipFolder(String zipFile, String outPath) throws Exception { ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry entry; String szName = ""; while ((entry = inZip.getNextEntry()) != null) { szName = entry.getName(); Log.e(TAG, "SZName:[ " + szName + "]"); if (entry.isDirectory()) { // get the folder name of the widget szName = szName.substring(0, szName.length() - 1); File folder = new File(outPath + File.separator + szName); Log.e(TAG, "目录: [" + folder.getAbsolutePath() + "]"); folder.mkdirs(); } else { File file = new File(outPath + File.separator + szName); File parentFile = file.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } Log.e(TAG, "全路径: [" + file.getAbsolutePath() + "]"); file.createNewFile(); // get the output stream of the file FileOutputStream out = new FileOutputStream(file); int len; byte[] buffer = new byte[1024]; // read (len) bytes into buffer while ((len = inZip.read(buffer)) != -1) { // write (len) byte from buffer at the position 0 out.write(buffer, 0, len); out.flush(); } out.close(); } } inZip.close(); } /** * Compress file and folder * * @param srcFileString file or folder to be Compress * @param zipFileString the path name of result ZIP * @throws Exception */ public static void ZipFolder(String srcFileString, String zipFileString) throws Exception { //create ZIP ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString)); //create the file File file = new File(srcFileString); //compress ZipFiles(file.getParent() + File.separator, file.getName(), outZip); //finish and close outZip.finish(); outZip.close(); } /** * compress files * * @param folderString * @param fileString * @param zipOutputSteam * @throws Exception */ private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam) throws Exception { if (zipOutputSteam == null) return; File file = new File(folderString + fileString); if (file.isFile()) { ZipEntry zipEntry = new ZipEntry(fileString); FileInputStream inputStream = new FileInputStream(file); zipOutputSteam.putNextEntry(zipEntry); int len; byte[] buffer = new byte[4096]; while ((len = inputStream.read(buffer)) != -1) { zipOutputSteam.write(buffer, 0, len); } zipOutputSteam.closeEntry(); } else { //folder String fileList[] = file.list(); //no child file and compress if (fileList.length <= 0) { ZipEntry zipEntry = new ZipEntry(fileString + File.separator); zipOutputSteam.putNextEntry(zipEntry); zipOutputSteam.closeEntry(); } //child files and recursion for (int i = 0; i < fileList.length; i++) { ZipFiles(folderString, fileString + File.separator + fileList[i], zipOutputSteam); }//end of for } } /** * return the InputStream of file in the ZIP * * @param zipFileString name of ZIP * @param fileString name of file in the ZIP * @return InputStream * @throws Exception */ public static InputStream UpZip(String zipFileString, String fileString) throws Exception { ZipFile zipFile = new ZipFile(zipFileString); ZipEntry zipEntry = zipFile.getEntry(fileString); return zipFile.getInputStream(zipEntry); } /** * return files list(file and folder) in the ZIP * * @param zipFileString ZIP name * @param bContainFolder contain folder or not * @param bContainFile contain file or not * @return * @throws Exception */ public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile) throws Exception { List<File> fileList = new ArrayList<File>(); ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); ZipEntry zipEntry; String szName = ""; while ((zipEntry = inZip.getNextEntry()) != null) { szName = zipEntry.getName(); if (zipEntry.isDirectory()) { // get the folder name of the widget szName = szName.substring(0, szName.length() - 1); File folder = new File(szName); if (bContainFolder) { fileList.add(folder); } } else { File file = new File(szName); if (bContainFile) { fileList.add(file); } } } inZip.close(); return fileList; } }