11package com .blankj .utilcode .utils ;
22
3- import android .content .Context ;
43import android .os .Environment ;
4+ import android .os .StatFs ;
55
66import java .io .File ;
77
@@ -20,7 +20,7 @@ private SDCardUtils() {
2020 }
2121
2222 /**
23- * 获取设备SD卡是否可用
23+ * 判断SD卡是否可用
2424 *
2525 * @return true : 可用<br>false : 不可用
2626 */
@@ -29,64 +29,67 @@ public static boolean isSDCardEnable() {
2929 }
3030
3131 /**
32- * 获取设备SD卡路径
32+ * 获取SD卡路径
3333 * <p>一般是/storage/emulated/0/</p>
3434 *
3535 * @return SD卡路径
3636 */
3737 public static String getSDCardPath () {
38- return Environment .getExternalStorageDirectory ().getAbsolutePath () + File .separator ;
38+ return Environment .getExternalStorageDirectory ().getPath () + File .separator ;
3939 }
4040
41- public static String getSDCardCacheDir (Context context ){
42- return context .getExternalCacheDir ().getPath ();
41+ /**
42+ * 获取SD卡Data路径
43+ *
44+ * @return Data路径
45+ */
46+ public static String getDataPath () {
47+ return Environment .getDataDirectory ().getPath ();
48+
4349 }
4450
45- // /**
46- // * 计算SD卡的剩余空间
47- // *
48- // * @return 返回-1,说明没有安装sd卡
49- // */
50- // public static long getFreeBytes(int unit) {
51- // long freeSpace = 0;
52- // if (isSDCardEnable()) {
53- // try {
54- // File path = Environment.getExternalStorageDirectory();
55- // StatFs stat = new StatFs(path.getPath());
56- // long blockSize = 0;
57- // long availableBlocks = 0;
58- // if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
59- // blockSize = stat.getBlockSizeLong();
60- // availableBlocks = stat.getAvailableBlocksLong();
61- // }
62- // freeSpace = (availableBlocks * blockSize) / unit;
63- // } catch (Exception e) {
64- // e.printStackTrace();
65- // }
66- // } else {
67- // return -1;
68- // }
69- // return (freeSpace);
70- // }
51+ /**
52+ * 获取系统存储路径
53+ *
54+ * @return 系统存储路径
55+ */
56+ public static String getRootDirectoryPath () {
57+ return Environment .getRootDirectory ().getAbsolutePath ();
58+ }
7159
60+ /**
61+ * 计算SD卡的剩余空间
62+ *
63+ * @param unit <ul>
64+ * <li>{@link ConstUtils#BYTE}: 字节</li>
65+ * <li>{@link ConstUtils#KB} : 千字节</li>
66+ * <li>{@link ConstUtils#MB} : 兆</li>
67+ * <li>{@link ConstUtils#GB} : GB</li>
68+ * </ul>
69+ * @return 返回-1,说明SD卡不可用,否则返回SD卡剩余空间
70+ */
71+ public static double getFreeSpace (int unit ) {
72+ if (isSDCardEnable ()) {
73+ try {
74+ StatFs stat = new StatFs (getSDCardPath ());
75+ long blockSize , availableBlocks ;
76+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .JELLY_BEAN_MR2 ) {
77+ availableBlocks = stat .getAvailableBlocksLong ();
78+ blockSize = stat .getBlockSizeLong ();
79+ } else {
80+ availableBlocks = stat .getAvailableBlocks ();
81+ blockSize = stat .getBlockSize ();
82+ }
83+ return FileUtils .byte2Unit (availableBlocks * blockSize , unit );
84+ } catch (Exception e ) {
85+ e .printStackTrace ();
86+ return -1.0 ;
87+ }
88+ } else {
89+ return -1.0 ;
90+ }
91+ }
7292
73- // /**
74- // * 获取SD卡的剩余容量 单位byte
75- // *
76- // * @return
77- // */
78- // public static long getSDCardAllSize() {
79- // if (isSDCardEnable()) {
80- // StatFs stat = new StatFs(getSDCardPath());
81- // // 获取空闲的数据块的数量
82- // long availableBlocks = (long) stat.getAvailableBlocks() - 4;
83- // // 获取单个数据块的大小(byte)
84- // long freeBlocks = stat.getAvailableBlocks();
85- // return freeBlocks * availableBlocks;
86- // }
87- // return 0;
88- // }
89- //
9093// /**
9194// * 获取指定路径所在空间的剩余可用容量字节数,单位byte
9295// *
0 commit comments