clear your own Cache Folder - Android Hardware

Android examples for Hardware:Cache Folder

Description

clear your own Cache Folder

Demo Code


//package com.java2s;

import android.content.Context;

import android.os.Environment;

import java.io.File;

public class Main {

    public static void clearCache(Context context) {
        File cacheDir = getCacheDir(context);
        if (cacheDir.exists()) {
            for (File file : cacheDir.listFiles()) {
                deleteFile(file);//from w  w w  .ja  v  a  2 s. c  o  m
            }
        }
    }

    public static File getCacheDir(Context context) {
        return isExternalStorageWritable() ? context.getExternalCacheDir()
                : context.getCacheDir();
    }

    private static void deleteFile(File root) {
        if (root.exists()) {
            if (root.isDirectory()) {
                for (File file : root.listFiles())
                    deleteFile(file);
            } else {
                root.delete();
            }
        }
    }

    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        return Environment.MEDIA_MOUNTED.equals(state);
    }
}

Related Tutorials