Android手机外置SD卡(TF卡)的获取方法
来源:互联网 发布:淘宝卖家开通闲鱼 编辑:程序博客网 时间:2024/04/29 01:13
From:http://www.2cto.com/kf/201407/320615.html
public class SDCardScanner {
2 /*
3 * avoid initializations of tool classes
4 */
5 private SDCardScanner() {
6 }
7
8 /**
9 * @Title: getExtSDCardPaths
10 * @Description: to obtain storage paths, the first path is theoretically
11 * the returned value of
12 * Environment.getExternalStorageDirectory(), namely the
13 * primary external storage. It can be the storage of internal
14 * device, or that of external sdcard. If paths.size() >1,
15 * basically, the current device contains two type of storage:
16 * one is the storage of the device itself, one is that of
17 * external sdcard. Additionally, the paths is directory.
18 * @return List<String>
19 * @throws IOException
20 */
21 public static List<String> getExtSDCardPaths() {
22 List<String> paths = new ArrayList<String>();
23 String extFileStatus = Environment.getExternalStorageState();
24 File extFile = Environment.getExternalStorageDirectory();
25 if (extFileStatus.endsWith(Environment.MEDIA_UNMOUNTED)
26 && extFile.exists() && extFile.isDirectory()
27 && extFile.canWrite()) {
28 paths.add(extFile.getAbsolutePath());
29 }
30 try {
31 // obtain executed result of command line code of 'mount', to judge
32 // whether tfCard exists by the result
33 Runtime runtime = Runtime.getRuntime();
34 Process process = runtime.exec("mount");
35 InputStream is = process.getInputStream();
36 InputStreamReader isr = new InputStreamReader(is);
37 BufferedReader br = new BufferedReader(isr);
38 String line = null;
39 int mountPathIndex = 1;
40 while ((line = br.readLine()) != null) {
41 // format of sdcard file system: vfat/fuse
42 if ((!line.contains("fat") && !line.contains("fuse") && !line
43 .contains("storage"))
44 || line.contains("secure")
45 || line.contains("asec")
46 || line.contains("firmware")
47 || line.contains("shell")
48 || line.contains("obb")
49 || line.contains("legacy") || line.contains("data")) {
50 continue;
51 }
52 String[] parts = line.split(" ");
53 int length = parts.length;
54 if (mountPathIndex >= length) {
55 continue;
56 }
57 String mountPath = parts[mountPathIndex];
58 if (!mountPath.contains("/") || mountPath.contains("data")
59 || mountPath.contains("Data")) {
60 continue;
61 }
62 File mountRoot = new File(mountPath);
63 if (!mountRoot.exists() || !mountRoot.isDirectory()
64 || !mountRoot.canWrite()) {
65 continue;
66 }
67 boolean equalsToPrimarySD = mountPath.equals(extFile
68 .getAbsolutePath());
69 if (equalsToPrimarySD) {
70 continue;
71 }
72 paths.add(mountPath);
73 }
74 } catch (IOException e) {
75 // TODO Auto-generated catch block
76 e.printStackTrace();
77 }
78 return paths;
79 }
80 }
复制代码
首先,我把它写成了一个工具类,因而声明了一个私有的构造器,目的就是要防止该类被实例化。
然后,首先获取了Android标准一部分的外置SD卡,如果它可用的话。
然后利用运行时,通过命令行函数"mount"来获取所有的存储位置,并对返回的结果进行SD卡或者TF卡的查找。
最后返回了所有可用于存储的不同的卡的位置,用一个List来保存。由于不是所有的手机都支持TF卡,因而这个List包含的路径未必很多,只有一个SD卡的手机只会返回一个路径,多个可用存储位置的会返回多个路径。
但有一点,是必须的,paths.get(0)肯定是外置SD卡的位置,因为它是primary external storage.
0 0
- Android手机外置SD卡(TF卡)的获取方法
- android 手机获取外置SD卡路径
- 获取手机机身和外置SD卡存储容量的方法
- Android获取内置和外置sd卡路径的方法
- android 获取外置SD卡的路径
- android获取外置SD卡的路径
- Android 获取外置SD卡
- 获取手机外置sd卡路径
- 获取外置的SD卡
- Android中获取内置SD卡和外置SD卡路径的方法
- Android 内部存储(外置SD卡)和扩展存储卡(TF卡)路径的获取
- Android开发中如何获取外置TF卡的路径
- Android开发中如何获取外置TF卡的路径
- Android获取机身存储、内置SD卡与外置TF卡路径
- Android获取不同手机 自身存储和外置SD卡存储路径的解决方案
- Android判断是否存在外置SD卡(获取手机所有存储设备的路径)
- Android 6.0获取外置SD卡路径方法
- 获取SD卡 tf卡 内置外置存储卡路径名称 路径的方式
- 性能展示报告的五分钟技巧
- 2014ACM多校训练4 HDOJ 4902 Nice boat
- 专访高磊:安卓APK安全加固的引领者
- 说说大型高并发高负载网站的系统架构(装逼用的,本人也看不懂)
- javascript事件特性检测代替浏览器嗅探
- Android手机外置SD卡(TF卡)的获取方法
- Windows GitHub 使用方法
- 内容观察者(一)
- IT忍者神龟之webwork EL
- 正则表达式30分钟入门教程
- Valid Sudoku
- 查找子串出现的次数以及索引
- 兼容IE 6/7 inline-block
- mysql字符集修改方法&中文乱码问题