Environment.getExternalStorageDirectory()。
如果手机本身有大容量存储,不论手机是否支持TF卡扩展,比如现在HTC系列高端手机One X,或者920 butterfly系列,那么获取到的是大容量存储的路径。也就是说,如果手机本身具有大容量存储,又支持TF卡扩展,通过这条命令是无法获取到TF卡信息的。
如下就说说怎样同时获取TF卡信息
工具类
public class SDcardInfo {
public final String HEAD = "dev_mount";
public final String LABEL = "<label>";
public final String MOUNT_POINT = "<mount_point>";
public final String PATH = "<part>";
public final String SYSFS_PATH = "<sysfs_path1...>";
private final int NLABEL = 1; // Label for the volume
private final int NPATH = 2; // The volume path
private final int NMOUNT_POINT = 3; // Where the volume will be mounted
private final int NSYSFS_PATH = 4;
private final int DEV_INTERNAL = 0;
private final int DEV_EXTERNAL = 1;
private ArrayList<String> cache = new ArrayList<String>();
private static SDcardInfo dev;
private final File VOLD_FSTAB = new File(Environment.getRootDirectory()
.getAbsoluteFile() + File.separator + "etc" + File.separator
+ "vold.fstab");
public static SDcardInfo getInstance() {
if (null == dev)
dev = new SDcardInfo();
return dev;
}
private DevInfo getInfo(final int device) {
DevInfo info = new DevInfo();
try {
initVoldFstabToCache();
} catch (IOException e) {
e.printStackTrace();
}
if (device >= cache.size()) {
return null;
}
String[] sinfo = cache.get(device).split(" ");
info.setLabel(sinfo[NLABEL]);
info.setMountPoint(sinfo[NMOUNT_POINT]);
info.setPath(sinfo[NPATH]);
info.setSysfsPath(sinfo[NSYSFS_PATH]);
return info;
}
/**
* init the words into the cache array
*
*/
private void initVoldFstabToCache() throws IOException {
cache.clear();
BufferedReader br = new BufferedReader(new FileReader(VOLD_FSTAB));
String tmp = null;
while ((tmp = br.readLine()) != null) {
// the words startsWith "dev_mount" are the SD info
if (tmp.startsWith(HEAD)) {
cache.add(tmp);
}
}
br.close();
cache.trimToSize();
}
public class DevInfo {
private String label, mountPoint, path, sysfsPath;
/**
* return the label name of the SD card
*/
public String getLabel() {
return label;
}
private void setLabel(String label) {
this.label = label;
}
/**
* the mount point of the SD card
*/
public String getMountPoint() {
return mountPoint;
}
private void setMountPoint(String mountPoint) {
this.mountPoint = mountPoint;
}
/**
* SD mount path
*/
public String getPath() {
return path;
}
private void setPath(String path) {
this.path = path;
}
/**
* "unknow"
*/
public String getSysfsPath() {
return sysfsPath;
}
private void setSysfsPath(String sysfs_path) {
this.sysfsPath = sysfsPath;
}
}
public DevInfo getInternalInfo() {
return getInfo(DEV_INTERNAL);
}
public DevInfo getExternalInfo() {
return getInfo(DEV_EXTERNAL);
}
}
工具类的使用
SDcardInfo dev1 = new SDcardInfo();
SDcardInfo dev2 = new SDcardInfo();
DevInfo internalInfo = dev1.getInternalInfo();
DevInfo externalInfo = dev2.getExternalInfo();
String internalPath ;
String externalPath;
if (internalInfo != null) {
internalPath = internalInfo.getPath(); // Internal path
}
if (externalInfo != null) {
externalPath = externalInfo.getPath();// External path
}
如果internalPath和externalPath 都为null ,则说明没有sdcard,如果externalPath 为 null 则说明没有外置卡。但当没有内置卡时插入TF卡,internalPath 不为 null,而externalPath为null ,说明此时的TF卡以内置卡的形式mount在机器内。