Skip to content

Commit c2c44d3

Browse files
committed
see 09/06 log
1 parent 25b75a2 commit c2c44d3

File tree

5 files changed

+198
-111
lines changed

5 files changed

+198
-111
lines changed

README-CN.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,11 +422,8 @@ isTablet : 判断是否是平板
422422

423423
* ### SD卡相关→[SDCardUtils.java][sdcard.java][Demo][sdcard.demo]
424424
```
425-
isSDCardEnable: 判断SD卡是否可用
426-
getSDCardPath : 获取SD卡路径
427-
getDataPath : 获取SD卡Data路径
428-
getFreeSpace : 计算SD卡的剩余空间
429-
getSDCardInfo : 获取SD卡信息
425+
isSDCardEnable : 判断SD卡是否可用
426+
getSDCardPaths : 获取SD卡路径
430427
```
431428

432429
* ### 服务相关→[ServiceUtils.java][service.java]

app/src/main/java/com/blankj/androidutilcode/core/sdcard/SDCardActivity.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.blankj.androidutilcode.base.BaseBackActivity;
1111
import com.blankj.utilcode.util.SDCardUtils;
1212

13+
import java.util.Arrays;
14+
1315
/**
1416
* <pre>
1517
* author: Blankj
@@ -41,10 +43,10 @@ public void initView(Bundle savedInstanceState, View view) {
4143

4244
TextView tvAboutSdcard = (TextView) findViewById(R.id.tv_about_sdcard);
4345
tvAboutSdcard.setText("isSDCardEnable: " + SDCardUtils.isSDCardEnable()
44-
+ "\ngetDataPath: " + SDCardUtils.getDataPath()
45-
+ "\ngetSDCardPath: " + SDCardUtils.getSDCardPath()
46-
+ "\ngetFreeSpace: " + SDCardUtils.getFreeSpace()
47-
+ "\ngetSDCardInfo: " + SDCardUtils.getSDCardInfo()
46+
+ "\ngetSDCardPaths: " + SDCardUtils.getSDCardPaths()
47+
+ "\ngetInnerSDCardPaths: " + SDCardUtils.getSDCardPaths(true)
48+
+ "\ngetOuterSDCardPaths: " + SDCardUtils.getSDCardPaths(false)
49+
+ "\ngetStorageDirectories: " + Arrays.asList(SDCardUtils.getStorageDirectories())
4850
);
4951
}
5052

update_log.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* 17/09/06 完善SDCardUtils获取SD卡路径,完善SPUtils新增commit
12
* 17/09/05 完善LogUtils,发布版本1.9.0
23
* 17/09/04 完善ToastUtils,去除相关safe函数,都改为safe实现,新增CustomToast的Demo
34
* 17/09/02 完善ToastUtils,去除引入view带来的问题,发布版本1.8.6
Lines changed: 54 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.blankj.utilcode.util;
22

3-
import android.annotation.TargetApi;
4-
import android.os.Build;
5-
import android.os.Environment;
6-
import android.os.StatFs;
3+
import android.content.Context;
4+
import android.os.storage.StorageManager;
75

8-
import java.io.BufferedInputStream;
9-
import java.io.BufferedReader;
10-
import java.io.File;
11-
import java.io.InputStreamReader;
6+
import java.lang.reflect.Array;
7+
import java.lang.reflect.InvocationTargetException;
8+
import java.lang.reflect.Method;
9+
import java.util.ArrayList;
10+
import java.util.Arrays;
11+
import java.util.List;
1212

1313
/**
1414
* <pre>
@@ -30,109 +30,69 @@ private SDCardUtils() {
3030
* @return true : 可用<br>false : 不可用
3131
*/
3232
public static boolean isSDCardEnable() {
33-
return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
33+
return !getSDCardPaths().isEmpty();
3434
}
3535

3636
/**
3737
* 获取SD卡路径
38-
* <p>先用shell,shell失败再普通方法获取,一般是/storage/emulated/0/</p>
3938
*
39+
* @param removable true : 外置SD卡<br>false : 内置SD卡
4040
* @return SD卡路径
4141
*/
42-
public static String getSDCardPath() {
43-
if (!isSDCardEnable()) return null;
44-
String cmd = "cat /proc/mounts";
45-
Runtime run = Runtime.getRuntime();
46-
BufferedReader bufferedReader = null;
42+
@SuppressWarnings("TryWithIdenticalCatches")
43+
public static List<String> getSDCardPaths(boolean removable) {
44+
List<String> paths = new ArrayList<>();
45+
StorageManager mStorageManager = (StorageManager) Utils.getApp()
46+
.getSystemService(Context.STORAGE_SERVICE);
4747
try {
48-
Process p = run.exec(cmd);
49-
bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream())));
50-
String lineStr;
51-
while ((lineStr = bufferedReader.readLine()) != null) {
52-
if (lineStr.contains("sdcard") && lineStr.contains(".android_secure")) {
53-
String[] strArray = lineStr.split(" ");
54-
if (strArray.length >= 5) {
55-
return strArray[1].replace("/.android_secure", "") + File.separator;
56-
}
57-
}
58-
if (p.waitFor() != 0 && p.exitValue() == 1) {
59-
break;
48+
Class<?> storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
49+
Method getVolumeList = StorageManager.class.getMethod("getVolumeList");
50+
Method getPath = storageVolumeClazz.getMethod("getPath");
51+
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
52+
Object result = getVolumeList.invoke(mStorageManager);
53+
final int length = Array.getLength(result);
54+
for (int i = 0; i < length; i++) {
55+
Object storageVolumeElement = Array.get(result, i);
56+
String path = (String) getPath.invoke(storageVolumeElement);
57+
boolean res = (Boolean) isRemovable.invoke(storageVolumeElement);
58+
if (removable == res) {
59+
paths.add(path);
6060
}
6161
}
62-
} catch (Exception e) {
62+
} catch (ClassNotFoundException e) {
63+
e.printStackTrace();
64+
} catch (InvocationTargetException e) {
65+
e.printStackTrace();
66+
} catch (NoSuchMethodException e) {
67+
e.printStackTrace();
68+
} catch (IllegalAccessException e) {
6369
e.printStackTrace();
64-
} finally {
65-
CloseUtils.closeIO(bufferedReader);
6670
}
67-
return Environment.getExternalStorageDirectory().getPath() + File.separator;
68-
}
69-
70-
/**
71-
* 获取SD卡data路径
72-
*
73-
* @return SD卡data路径
74-
*/
75-
public static String getDataPath() {
76-
if (!isSDCardEnable()) return null;
77-
return Environment.getExternalStorageDirectory().getPath() + File.separator + "data" + File.separator;
71+
return paths;
7872
}
7973

8074
/**
81-
* 获取SD卡剩余空间
82-
*
83-
* @return SD卡剩余空间
84-
*/
85-
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
86-
public static String getFreeSpace() {
87-
if (!isSDCardEnable()) return null;
88-
StatFs stat = new StatFs(getSDCardPath());
89-
long blockSize, availableBlocks;
90-
availableBlocks = stat.getAvailableBlocksLong();
91-
blockSize = stat.getBlockSizeLong();
92-
return ConvertUtils.byte2FitMemorySize(availableBlocks * blockSize);
93-
}
94-
95-
/**
96-
* 获取SD卡信息
75+
* 获取SD卡路径
9776
*
98-
* @return SDCardInfo
77+
* @return SD卡路径
9978
*/
100-
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
101-
public static String getSDCardInfo() {
102-
if (!isSDCardEnable()) return null;
103-
SDCardInfo sd = new SDCardInfo();
104-
sd.isExist = true;
105-
StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath());
106-
sd.totalBlocks = sf.getBlockCountLong();
107-
sd.blockByteSize = sf.getBlockSizeLong();
108-
sd.availableBlocks = sf.getAvailableBlocksLong();
109-
sd.availableBytes = sf.getAvailableBytes();
110-
sd.freeBlocks = sf.getFreeBlocksLong();
111-
sd.freeBytes = sf.getFreeBytes();
112-
sd.totalBytes = sf.getTotalBytes();
113-
return sd.toString();
114-
}
115-
116-
public static class SDCardInfo {
117-
boolean isExist;
118-
long totalBlocks;
119-
long freeBlocks;
120-
long availableBlocks;
121-
long blockByteSize;
122-
long totalBytes;
123-
long freeBytes;
124-
long availableBytes;
125-
126-
@Override
127-
public String toString() {
128-
return "isExist=" + isExist +
129-
"\ntotalBlocks=" + totalBlocks +
130-
"\nfreeBlocks=" + freeBlocks +
131-
"\navailableBlocks=" + availableBlocks +
132-
"\nblockByteSize=" + blockByteSize +
133-
"\ntotalBytes=" + totalBytes +
134-
"\nfreeBytes=" + freeBytes +
135-
"\navailableBytes=" + availableBytes;
79+
@SuppressWarnings("TryWithIdenticalCatches")
80+
public static List<String> getSDCardPaths() {
81+
StorageManager storageManager = (StorageManager) Utils.getApp()
82+
.getSystemService(Context.STORAGE_SERVICE);
83+
List<String> paths = new ArrayList<>();
84+
try {
85+
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths");
86+
getVolumePathsMethod.setAccessible(true);
87+
Object invoke = getVolumePathsMethod.invoke(storageManager);
88+
paths = Arrays.asList((String[]) invoke);
89+
} catch (NoSuchMethodException e) {
90+
e.printStackTrace();
91+
} catch (IllegalAccessException e) {
92+
e.printStackTrace();
93+
} catch (InvocationTargetException e) {
94+
e.printStackTrace();
13695
}
96+
return paths;
13797
}
13898
}

0 commit comments

Comments
 (0)