1
1
package com .blankj .utilcode .utils ;
2
2
3
- import android .content .Context ;
4
3
import android .os .Environment ;
4
+ import android .os .StatFs ;
5
5
6
6
import java .io .File ;
7
7
@@ -20,7 +20,7 @@ private SDCardUtils() {
20
20
}
21
21
22
22
/**
23
- * 获取设备SD卡是否可用
23
+ * 判断SD卡是否可用
24
24
*
25
25
* @return true : 可用<br>false : 不可用
26
26
*/
@@ -29,64 +29,67 @@ public static boolean isSDCardEnable() {
29
29
}
30
30
31
31
/**
32
- * 获取设备SD卡路径
32
+ * 获取SD卡路径
33
33
* <p>一般是/storage/emulated/0/</p>
34
34
*
35
35
* @return SD卡路径
36
36
*/
37
37
public static String getSDCardPath () {
38
- return Environment .getExternalStorageDirectory ().getAbsolutePath () + File .separator ;
38
+ return Environment .getExternalStorageDirectory ().getPath () + File .separator ;
39
39
}
40
40
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
+
43
49
}
44
50
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
+ }
71
59
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
+ }
72
92
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
- //
90
93
// /**
91
94
// * 获取指定路径所在空间的剩余可用容量字节数,单位byte
92
95
// *
0 commit comments