Android手机外置SD卡(TF卡)的获取方法
Android手机上的外置SD卡,起初的时候,即在Android出世的前几年,那时手机的存储是十分有限的,不像现在到处可见16G、32G和64G的存储,因而那时候的手机有的厂商允许插入外置的SD卡,此时这张卡仍处于手机的扩展部分。后来,随着手机的发展以及存储能力的增加,这张外置SD卡,逐渐成为了手机的一部分,不再允许可插拔了,当然现在依然有的手机允许对存储进行拓展,比如三星等。
那张拓展的存储卡,现在叫做TF卡,且不是所有的手机都支持它,但是有时候有些奇葩需求偏要优先存储在TF卡里面,这叫不得不要求开发人员去检查这张卡是否存在、是否可用。又因为这是手机厂商可拓展、可自定义的部分,所有不同厂商生产的手机,以及同一厂商生产的不同型号的手机,TF卡的位置都相差很大,并没有一个统一的名称或位置。因而这是比较困难的一部分,但是还好Android是开源的,我们可以通过运行时来判断手机是否有TF卡,以及TF卡是否可用。
下面这个方法可以获取手机的可以存储,包括SD卡、TF卡等,对多存储卡进行了匹配,详细的代码如下:
public class SDCardScanner {
/*
* avoid initializations of tool classes
*/
private SDCardScanner() {
} /**
* @Title: getExtSDCardPaths
* @Description: to obtain storage paths, the first path is theoretically
* the returned value of
* Environment.getExternalStorageDirectory(), namely the
* primary external storage. It can be the storage of internal
* device, or that of external sdcard. If paths.size() >1,
* basically, the current device contains two type of storage:
* one is the storage of the device itself, one is that of
* external sdcard. Additionally, the paths is directory.
* @return List<String>
* @throws IOException
*/
public static List<String> getExtSDCardPaths() {
List<String> paths = new ArrayList<String>();
String extFileStatus = Environment.getExternalStorageState();
File extFile = Environment.getExternalStorageDirectory();
if (extFileStatus.equals(Environment.MEDIA_MOUNTED)
&& extFile.exists() && extFile.isDirectory()
&& extFile.canWrite()) {
paths.add(extFile.getAbsolutePath());
}
try {
// obtain executed result of command line code of 'mount', to judge
// whether tfCard exists by the result
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("mount");
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
int mountPathIndex = 1;
while ((line = br.readLine()) != null) {
// format of sdcard file system: vfat/fuse
if ((!line.contains("fat") && !line.contains("fuse") && !line
.contains("storage"))
|| line.contains("secure")
|| line.contains("asec")
|| line.contains("firmware")
|| line.contains("shell")
|| line.contains("obb")
|| line.contains("legacy") || line.contains("data")) {
continue;
}
String[] parts = line.split(" ");
int length = parts.length;
if (mountPathIndex >= length) {
continue;
}
String mountPath = parts[mountPathIndex];
if (!mountPath.contains("/") || mountPath.contains("data")
|| mountPath.contains("Data")) {
continue;
}
File mountRoot = new File(mountPath);
if (!mountRoot.exists() || !mountRoot.isDirectory()
|| !mountRoot.canWrite()) {
continue;
}
boolean equalsToPrimarySD = mountPath.equals(extFile
.getAbsolutePath());
if (equalsToPrimarySD) {
continue;
}
paths.add(mountPath);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return paths;
}
}
首先,我把它写成了一个工具类,因而声明了一个私有的构造器,目的就是要防止该类被实例化。
然后,首先获取了Android标准一部分的外置SD卡,如果它可用的话。
然后利用运行时,通过命令行函数"mount"来获取所有的存储位置,并对返回的结果进行SD卡或者TF卡的查找。
最后返回了所有可用于存储的不同的卡的位置,用一个List来保存。由于不是所有的手机都支持TF卡,因而这个List包含的路径未必很多,只有一个SD卡的手机只会返回一个路径,多个可用存储位置的会返回多个路径。
但有一点,是必须的,paths.get(0)肯定是外置SD卡的位置,因为它是primary external storage.
这是一篇有关Android存储的文章,推荐给大家,有兴趣的可以查看一下:Android存储的概念辨析及使用说明
Android手机外置SD卡(TF卡)的获取方法的更多相关文章
- Keil MDK STM32系列(九) 基于HAL和FatFs的FAT格式SD卡TF卡读写
Keil MDK STM32系列 Keil MDK STM32系列(一) 基于标准外设库SPL的STM32F103开发 Keil MDK STM32系列(二) 基于标准外设库SPL的STM32F401 ...
- 开发中,android手机WIFI无法使用,无SIM卡故障解决
用eclipse 开发android中,突然出现,android手机WIFI无法使用,无SIM卡故障解决 发现故障后,想办法刷机(没有成功),触点清洁都搞了. 最后恢复出厂设置居然解决了,留资料给同行 ...
- 联盛德 HLK-W806 (十三): 运行FatFs读写FAT和exFat格式的SD卡/TF卡
目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...
- Android操作外置SD卡和U盘相关文章
Android设备与外接U盘实现数据读取操作https://blog.csdn.net/true100/article/details/77775700 usbdisklibhttps://githu ...
- Android5.0以后版本把应用移动到SD或者TF卡的方法
由于手机内存较小,才8G,用的时间一久,内部存储就满了,天天删垃圾,WIFI还老断线,终于忍无可忍了,决定把应用移动到SD卡,实践下来,只有少部分App默认支持移动到SD卡,大部分程序不支持只能装在内 ...
- Android手机自带内部存储路径的获取 (转)
转自:http://my.oschina.net/liucundong/blog/288183 我有一台中兴的Android手机,型号是 ZTE U930HD,手机没有插入外置SD卡(也就是Micro ...
- Linux 最小系统挂载U盘(SD、TF卡)并执行程序
一.在Ubuntu下编译C文件 使用指令"arm-none-linux-gnueabi-gcc-4.4.1 -o HelloWorld HelloWorld.c -static"编 ...
- SD卡 TF卡 接口引脚定义
- android手机操作SD的使用方法
写入SD卡 package com.example.openfileproject; import java.io.File; import java.io.FileInputStream; impo ...
随机推荐
- Codeforces 448 D. Multiplication Table
二分法判断答案 D. Multiplication Table time limit per test 1 second memory limit per test 256 megabytes inp ...
- avalon与双缓冲技术
avalon与双缓冲技术 avalon1.5一个重要技术升级是引进异步渲染.异步渲染在游戏界有一个更专业的名字,叫双缓冲.游戏界要刷新界面与我们刷新浏览器视图,面临的问题是一致的.视图是由许多存在套嵌 ...
- JavaScript中null和undefined的总结
先说null,它表示一个特殊值,常用来描述“空值”.对null执行typeof运算,结果返回字符串“object”,也就是说,可以将null认为是一个特殊的对象值,含义是“非对象”(感觉怪怪的).实际 ...
- 怎样批量把excel中已显示的科学计数法取消
作者:iamlaosong 把一文本文档拷贝到EXCEL中时,当中一列数字所有变成科学计数法,这些数事实上是条码号,不需进行运算,怎样能够取消科学计算法,将数字显示成原来的样子呢?一般方法例如以下: ...
- 《Javascript权威指南》十六学习笔记:BOM资源---BOM基本应用
BOM基本应用包括:管理浏览器历史记录.得到处理和解决浏览器的信息.本文介绍了这些应用程序. 一.浏览历史管理 1.history对象的方法和属性 History 对象包括用户(在浏览器窗体中)訪问过 ...
- hdu 1700 Points on Cycle 水几何
已知圆心(0,0)圆周上的一点,求圆周上另外两点使得三点构成等边三角形. 懒得推公式,直接用模板2圆(r1=dist,r2=sqrt(3)*dist)相交水过 #include<cstdio&g ...
- MyEclipse10.0 集成 SVN
一:下载服务端和client工具 服务端安装工具:Setup-Subversion-1.6.5.msi client安装工具:TortoiseSVN 下载地址:http://subclipse.t ...
- Windows在配置Python+tornado
1,安装Python 2.7.x版本号 地址:https://www.python.org/downloads/release/python-278/ 2,安装python setuptools工具 ...
- windows已安装solr
下载地址:http://archive.apache.org/dist/lucene/solr/ 操作环境: Win7,Tomcat6, Solr4.3, Jdk6 下载solr4.3的包,解压到本 ...
- 图解zookeeper FastLeader选举算法
zookeeper当配置为群集模式,在启动或异常情况将被选举为的例子Leader.默认选择算法FastLeaderElection. 不知道zookeeper够考虑这样一个问题:某个服务能够配置为多个 ...