|
@@ -0,0 +1,173 @@
|
|
|
+package com.ynstkz.shitu.android.utils;
|
|
|
+
|
|
|
+import android.os.Environment;
|
|
|
+import android.util.Log;
|
|
|
+
|
|
|
+import com.ynstkz.shitu.android.application.STApplication;
|
|
|
+import com.ynstkz.shitu.android.common.Constants;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 作者:fuchangle on 2018/3/1 13:42
|
|
|
+ */
|
|
|
+
|
|
|
+public class FileUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否存在内存卡判断
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean existSDcard() {
|
|
|
+ if (Environment.MEDIA_MOUNTED.equals(Environment
|
|
|
+ .getExternalStorageState())) {
|
|
|
+ return true;
|
|
|
+ } else
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取APP的根目录
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getAppRootPath(){
|
|
|
+ String path = STApplication.instance.getApplicationContext().getExternalFilesDir(null)+ File.separator;
|
|
|
+ File file = new File(path);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ return path;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图片缓存路径
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getImageCachePath(){
|
|
|
+ if(existSDcard()){
|
|
|
+ String path = getAppRootPath() + Constants.DOWNLOAD_IMAGE_NAME+ File.separator;
|
|
|
+ File file = new File(path);
|
|
|
+ if(!file.exists()){
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ return path;
|
|
|
+ } else {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取缓存文件夹大小
|
|
|
+ */
|
|
|
+ public static String getFolderSize(String filePath){
|
|
|
+ File file=new File(filePath);
|
|
|
+ long blockSize=0;
|
|
|
+ try {
|
|
|
+ if(file.isDirectory()){
|
|
|
+ blockSize = getFileSizes(file);
|
|
|
+ } else {
|
|
|
+ blockSize = getFileSize(file);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return FormetFileSize(blockSize);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定文件大小
|
|
|
+ * @param file
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @SuppressWarnings("resource")
|
|
|
+ private static long getFileSize(File file) throws Exception {
|
|
|
+ long size = 0;
|
|
|
+ if (file.exists()){
|
|
|
+ FileInputStream fis = null;
|
|
|
+ fis = new FileInputStream(file);
|
|
|
+ size = fis.available();
|
|
|
+ fis.close();
|
|
|
+ } else {
|
|
|
+ file.createNewFile();
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定文件夹大小
|
|
|
+ * @param f
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ private static long getFileSizes(File f) throws Exception {
|
|
|
+ long size = 0;
|
|
|
+ File flist[] = f.listFiles();
|
|
|
+ for (int i = 0; i < flist.length; i++){
|
|
|
+ if (flist[i].isDirectory()){
|
|
|
+ size = size + getFileSizes(flist[i]);
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ size =size + getFileSize(flist[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return size;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 大小单位换算
|
|
|
+ * @param fileS
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static String FormetFileSize(long fileS) {
|
|
|
+ DecimalFormat df = new DecimalFormat("#.00");
|
|
|
+ String fileSizeString = "";
|
|
|
+ String wrongSize="0B";
|
|
|
+ if(fileS==0){
|
|
|
+ return wrongSize;
|
|
|
+ }
|
|
|
+ if (fileS < 1024){
|
|
|
+ fileSizeString = df.format((double) fileS) + "B";
|
|
|
+ }
|
|
|
+ else if (fileS < 1048576){
|
|
|
+ fileSizeString = df.format((double) fileS / 1024) + "KB";
|
|
|
+ }
|
|
|
+ else if (fileS < 1073741824){
|
|
|
+ fileSizeString = df.format((double) fileS / 1048576) + "MB";
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ fileSizeString = df.format((double) fileS / 1073741824) + "GB";
|
|
|
+ }
|
|
|
+ return fileSizeString;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 递归删除目录下的所有文件及子目录下所有文件
|
|
|
+ * @param dir 将要删除的文件目录
|
|
|
+ * @return boolean Returns "true" if all deletions were successful.
|
|
|
+ * If a deletion fails, the method stops attempting to
|
|
|
+ * delete and returns "false".
|
|
|
+ */
|
|
|
+ public static void deleteDir(String dir) {
|
|
|
+ File file = new File(dir);
|
|
|
+ if (file.exists()) {
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ File[] files = file.listFiles();
|
|
|
+ for (File subFile : files) {
|
|
|
+ if (subFile.isDirectory()) {
|
|
|
+ deleteDir(subFile.getPath());
|
|
|
+ } else {
|
|
|
+ subFile.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!file.getName().equals("files")) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|