FileUtil.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package com.xzjmyk.pm.activity.util;
  2. import android.os.Environment;
  3. import android.text.TextUtils;
  4. import com.xzjmyk.pm.activity.bean.User;
  5. import com.xzjmyk.pm.activity.MyApplication;
  6. import java.io.File;
  7. import java.util.UUID;
  8. public class FileUtil {
  9. private static final int TYPE_IMAGE = 1;
  10. private static final int TYPE_ADUIO = 2;
  11. private static final int TYPE_VIDEO = 3;
  12. /**
  13. * {@link #TYPE_IMAGE}<br/>
  14. * {@link #TYPE_ADUIO}<br/>
  15. * {@link #TYPE_VIDEO} <br/>
  16. *
  17. * @param type
  18. * @return
  19. */
  20. private static String getPublicFilePath(int type) {
  21. String fileDir = null;
  22. String fileSuffix = null;
  23. switch (type) {
  24. case TYPE_ADUIO:
  25. fileDir = MyApplication.getInstance().mVoicesDir;
  26. fileSuffix = ".mp3";
  27. break;
  28. case TYPE_VIDEO:
  29. fileDir = MyApplication.getInstance().mVideosDir;
  30. fileSuffix = ".mp4";
  31. break;
  32. case TYPE_IMAGE:
  33. fileDir = MyApplication.getInstance().mPicturesDir;
  34. fileSuffix = ".jpg";
  35. break;
  36. }
  37. if (fileDir == null) {
  38. return null;
  39. }
  40. File file = new File(fileDir);
  41. if (!file.exists()) {
  42. if (!file.mkdirs()) {
  43. return null;
  44. }
  45. }
  46. return fileDir + File.separator + UUID.randomUUID().toString().replaceAll("-", "") + fileSuffix;
  47. }
  48. /**
  49. * {@link #TYPE_ADUIO}<br/>
  50. * {@link #TYPE_VIDEO} <br/>
  51. *
  52. * @param type
  53. * @return
  54. */
  55. private static String getPrivateFilePath(int type, String userId) {
  56. String fileDir = null;
  57. String fileSuffix = null;
  58. switch (type) {
  59. case TYPE_ADUIO:
  60. fileDir = MyApplication.getInstance().mAppDir + File.separator + userId + File.separator + Environment.DIRECTORY_MUSIC;
  61. fileSuffix = ".mp3";
  62. break;
  63. case TYPE_VIDEO:
  64. fileDir = MyApplication.getInstance().mAppDir + File.separator + userId + File.separator + Environment.DIRECTORY_MOVIES;
  65. fileSuffix = ".mp4";
  66. break;
  67. }
  68. if (fileDir == null) {
  69. return null;
  70. }
  71. File file = new File(fileDir);
  72. if (!file.exists()) {
  73. if (!file.mkdirs()) {
  74. return null;
  75. }
  76. }
  77. return fileDir + File.separator + UUID.randomUUID().toString().replaceAll("-", "") + fileSuffix;
  78. }
  79. public static String getRandomImageFilePath() {
  80. return getPublicFilePath(TYPE_IMAGE);
  81. }
  82. public static String getRandomAudioFilePath() {
  83. User user = MyApplication.getInstance().mLoginUser;
  84. if (user != null && !TextUtils.isEmpty(user.getUserId())) {
  85. return getPrivateFilePath(TYPE_ADUIO, user.getUserId());
  86. } else {
  87. return getPublicFilePath(TYPE_ADUIO);
  88. }
  89. }
  90. public static String getRandomAudioAmrFilePath() {
  91. User user = MyApplication.getInstance().mLoginUser;
  92. String filePath = null;
  93. if (user != null && !TextUtils.isEmpty(user.getUserId())) {
  94. filePath = getPrivateFilePath(TYPE_ADUIO, user.getUserId());
  95. } else {
  96. filePath = getPublicFilePath(TYPE_ADUIO);
  97. }
  98. if (!TextUtils.isEmpty(filePath)) {
  99. return filePath.replace(".mp3", ".amr");
  100. } else {
  101. return null;
  102. }
  103. }
  104. public static String getRandomVideoFilePath() {
  105. User user = MyApplication.getInstance().mLoginUser;
  106. if (user != null && !TextUtils.isEmpty(user.getUserId())) {
  107. return getPrivateFilePath(TYPE_VIDEO, user.getUserId());
  108. } else {
  109. return getPublicFilePath(TYPE_VIDEO);
  110. }
  111. }
  112. // ///////////////////////////////////////////////////////////////////////////////////////////////////
  113. public static void createFileDir(String fileDir) {
  114. File fd = new File(fileDir);
  115. if (!fd.exists()) {
  116. fd.mkdirs();
  117. }
  118. }
  119. /**
  120. *
  121. * @param fullName
  122. */
  123. public static void delFile(String fullName) {
  124. File file = new File(fullName);
  125. if (file.exists()) {
  126. if (file.isFile()) {
  127. try {
  128. file.delete();
  129. } catch (Exception e) {
  130. e.printStackTrace();
  131. }
  132. }
  133. }
  134. }
  135. /**
  136. * 删除文件夹里面的所有文件
  137. *
  138. * @param path
  139. * String 文件夹路径 如 /sdcard/data/
  140. */
  141. public static void delAllFile(String path) {
  142. File file = new File(path);
  143. if (!file.exists()) {
  144. return;
  145. }
  146. if (!file.isDirectory()) {
  147. return;
  148. }
  149. String[] tempList = file.list();
  150. File temp = null;
  151. for (int i = 0; i < tempList.length; i++) {
  152. System.out.println(path + tempList[i]);
  153. if (path.endsWith(File.separator)) {
  154. temp = new File(path + tempList[i]);
  155. } else {
  156. temp = new File(path + File.separator + tempList[i]);
  157. }
  158. if (temp.isFile()) {
  159. temp.delete();
  160. }
  161. if (temp.isDirectory()) {
  162. delAllFile(path + "/" + tempList[i]); // 先删除文件夹里面的文件
  163. delFolder(path + "/" + tempList[i]); // 再删除空文件夹
  164. }
  165. }
  166. }
  167. /**
  168. * 删除文件夹
  169. *
  170. * String 文件夹路径及名称 如/sdcard/data/
  171. * String
  172. * @return boolean
  173. */
  174. public static void delFolder(String folderPath) {
  175. try {
  176. delAllFile(folderPath); // 删除完里面所有内容
  177. String filePath = folderPath;
  178. filePath = filePath.toString();
  179. File myFilePath = new File(filePath);
  180. myFilePath.delete(); // 删除空文件夹
  181. } catch (Exception e) {
  182. System.out.println("删除文件夹操作出错");
  183. e.printStackTrace();
  184. }
  185. }
  186. }