StorageUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package com.yc.videocache;
  2. import android.content.Context;
  3. import android.os.Environment;
  4. import java.io.File;
  5. import static android.os.Environment.MEDIA_MOUNTED;
  6. /**
  7. * Provides application storage paths
  8. * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)
  9. * @since 1.0.0
  10. */
  11. public final class StorageUtils {
  12. private static final String INDIVIDUAL_DIR_NAME = "video-cache";
  13. /**
  14. * Returns individual application cache directory (for only video caching from Proxy). Cache directory will be
  15. * created on SD card <i>("/Android/data/[app_package_name]/cache/video-cache")</i> if card is mounted .
  16. * Else - Android defines cache directory on device's file system.
  17. *
  18. * @param context Application context
  19. * @return Cache {@link File directory}
  20. */
  21. static File getIndividualCacheDirectory(Context context) {
  22. File cacheDir = getCacheDirectory(context);
  23. return new File(cacheDir, INDIVIDUAL_DIR_NAME);
  24. }
  25. /**
  26. * Returns application cache directory. Cache directory will be created on SD card
  27. * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
  28. * on device's file system depending incoming parameters.
  29. *
  30. * @param context Application context
  31. * @return Cache {@link File directory}.<br />
  32. * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
  33. * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
  34. */
  35. private static File getCacheDirectory(Context context) {
  36. File appCacheDir = null;
  37. if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
  38. appCacheDir = context.getExternalCacheDir();
  39. }
  40. if (appCacheDir == null) {
  41. appCacheDir = context.getCacheDir();
  42. }
  43. if (appCacheDir == null) {
  44. String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
  45. appCacheDir = new File(cacheDirPath);
  46. }
  47. return appCacheDir;
  48. }
  49. /**
  50. * 删除文件
  51. * @param root file
  52. * @return 是否删除成功
  53. */
  54. public static boolean deleteFiles(File root) {
  55. File[] files = root.listFiles();
  56. if (files != null) {
  57. for (File f : files) {
  58. if (!f.isDirectory() && f.exists()) { // 判断是否存在
  59. if (!f.delete()) {
  60. return false;
  61. }
  62. }
  63. }
  64. }
  65. return true;
  66. }
  67. /**
  68. * 删除文件
  69. * @param filePath file路径
  70. * @return 是否删除成功
  71. */
  72. public static boolean deleteFile(String filePath) {
  73. File file = new File(filePath);
  74. if (file.exists()) {
  75. if (file.isFile()) {
  76. return file.delete();
  77. } else {
  78. String[] filePaths = file.list();
  79. if (filePaths != null) {
  80. for (String path : filePaths) {
  81. deleteFile(filePath + File.separator + path);
  82. }
  83. }
  84. return file.delete();
  85. }
  86. }
  87. return true;
  88. }
  89. }