123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package com.yc.videocache;
- import android.content.Context;
- import android.os.Environment;
- import java.io.File;
- import static android.os.Environment.MEDIA_MOUNTED;
- /**
- * Provides application storage paths
- * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
- * @since 1.0.0
- */
- public final class StorageUtils {
- private static final String INDIVIDUAL_DIR_NAME = "video-cache";
- /**
- * Returns individual application cache directory (for only video caching from Proxy). Cache directory will be
- * created on SD card <i>("/Android/data/[app_package_name]/cache/video-cache")</i> if card is mounted .
- * Else - Android defines cache directory on device's file system.
- *
- * @param context Application context
- * @return Cache {@link File directory}
- */
- static File getIndividualCacheDirectory(Context context) {
- File cacheDir = getCacheDirectory(context);
- return new File(cacheDir, INDIVIDUAL_DIR_NAME);
- }
- /**
- * Returns application cache directory. Cache directory will be created on SD card
- * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
- * on device's file system depending incoming parameters.
- *
- * @param context Application context
- * @return Cache {@link File directory}.<br />
- * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
- * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
- */
- private static File getCacheDirectory(Context context) {
- File appCacheDir = null;
- if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
- appCacheDir = context.getExternalCacheDir();
- }
- if (appCacheDir == null) {
- appCacheDir = context.getCacheDir();
- }
- if (appCacheDir == null) {
- String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
- appCacheDir = new File(cacheDirPath);
- }
- return appCacheDir;
- }
- /**
- * 删除文件
- * @param root file
- * @return 是否删除成功
- */
- public static boolean deleteFiles(File root) {
- File[] files = root.listFiles();
- if (files != null) {
- for (File f : files) {
- if (!f.isDirectory() && f.exists()) { // 判断是否存在
- if (!f.delete()) {
- return false;
- }
- }
- }
- }
- return true;
- }
- /**
- * 删除文件
- * @param filePath file路径
- * @return 是否删除成功
- */
- public static boolean deleteFile(String filePath) {
- File file = new File(filePath);
- if (file.exists()) {
- if (file.isFile()) {
- return file.delete();
- } else {
- String[] filePaths = file.list();
- if (filePaths != null) {
- for (String path : filePaths) {
- deleteFile(filePath + File.separator + path);
- }
- }
- return file.delete();
- }
- }
- return true;
- }
- }
|