菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
57
0

Android读取sd卡

原创
05/13 14:22
阅读数 96522
 1     public static String[] getStoragePaths() {
 2         List<String> pathsList = new ArrayList<String>();
 3         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD) {
 4             File externalFolder = Environment.getExternalStorageDirectory();
 5             if (externalFolder != null) {
 6                 pathsList.add(externalFolder.getAbsolutePath());
 7             }
 8         } else {
 9             StorageManager storageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
10             try {
11                 Method method = StorageManager.class.getDeclaredMethod("getVolumePaths");
12                 method.setAccessible(true);
13                 Object result = method.invoke(storageManager);
14                 if (result != null && result instanceof String[]) {
15                     String[] paths = (String[]) result;
16                     StatFs statFs;
17                     for (String path : paths) {
18                         if (!TextUtils.isEmpty(path) && new File(path).exists()) {
19                             statFs = new StatFs(path);
20                             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
21                                 if (statFs.getBlockCountLong() * statFs.getBlockSizeLong() != 0) {
22                                     pathsList.add(path);
23                                 }
24                             }else{
25                                 if (statFs.getBlockCount() * statFs.getBlockSize() != 0) {
26                                     pathsList.add(path);
27                                 }
28                             }
29                         }
30                     }
31                 }
32             } catch (Exception e) {
33                 File externalFolder = Environment.getExternalStorageDirectory();
34                 if (externalFolder != null) {
35                     pathsList.add(externalFolder.getAbsolutePath());
36                 }
37             }
38         }
39         return pathsList.toArray(new String[pathsList.size()]);
40     }

 

发表评论

0/200
57 点赞
0 评论
收藏