android 未提供获取外置SD储存卡的相应函数或方法,但我们可以自己写一个。当前只能用Environment.getExternalStorageDirectory()获取内置的SD卡路径,因为不同机型的系统SD卡的路径不相同,但是我们仍然可以有方法去获得外置SD卡的路径, 内置和外置SD卡的信息存在system/etc/vold.fstab 里面,我们可以从这里获得外置SD卡的路径, 这里面的内容就算在设备非ROOT的情况下也是可读的,所以这个方法值得一试:
本人写好了一个类,可供直接使用或参考:
Dev_MountInfo.class
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import com.snuabar.getmounteddevices.Dev_MountInfo.DevInfo;
import android.os.Environment;
public class Dev_MountInfo implements IDev {
/**
* ***
*/
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...>";
/**
* Label for the volume
*/
private final int NLABEL = 1;
/**
* Partition
*/
private final int NPATH = 2;
/**
* Where the volume will be mounted
*/
private final int NMOUNT_POINT = 3;
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 Dev_MountInfo dev;
private DevInfo info;
private final File VOLD_FSTAB = new File(Environment.getRootDirectory()
.getAbsoluteFile()
+ File.separator
+ "etc"
+ File.separator
+ "vold.fstab");
public static Dev_MountInfo getInstance() {
if (null == dev)
dev = new Dev_MountInfo();
return dev;
}
private DevInfo getInfo(final int device) {
// for(String str:cache)
// System.out.println(str);
if (null == info)
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.setMount_point(sinfo[NMOUNT_POINT]);
info.setPath(sinfo[NPATH]);
info.setSysfs_path(sinfo[NSYSFS_PATH]);
return info;
}
/**
* init the words into the cache array
* @throws IOException
*/
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, mount_point, path, sysfs_path;
/**
* return the label name of the SD card
* @return
*/
public String getLabel() {
return label;
}
private void setLabel(String label) {
this.label = label;
}
/**
* the mount point of the SD card
* @return
*/
public String getMount_point() {
return mount_point;
}
private void setMount_point(String mount_point) {
this.mount_point = mount_point;
}
/**
* SD mount path
* @return
*/
public String getPath() {
return path;
}
private void setPath(String path) {
this.path = path;
}
/**
* "unknow"
* @return
*/
public String getSysfs_path() {
return sysfs_path;
}
private void setSysfs_path(String sysfs_path) {
this.sysfs_path = sysfs_path;
}
}
@Override
public DevInfo getInternalInfo() {
return getInfo(DEV_INTERNAL);
}
@Override
public DevInfo getExternalInfo() {
return getInfo(DEV_EXTERNAL);
}
}
interface IDev {
DevInfo getInternalInfo();
DevInfo getExternalInfo();
}
使用方法:
[java] view plain copy print ?
- Dev_MountInfo dev = Dev_MountInfo.getInstance();
- DevInfo info = dev.getInternalInfo();
- info = dev.getExternalInfo();
-
-
- info.getLabel();
- info.getMount_point();
- info.getPath();
- info.getSysfs_path();
Dev_MountInfo dev = Dev_MountInfo.getInstance();
DevInfo info = dev.getInternalInfo();//Internal SD Card Informations
info = dev.getExternalInfo();//External SD Card Informations
// Methods:
info.getLabel(); // SD 卡的名称
info.getMount_point();//SD 卡挂载点
info.getPath(); //SD 卡路径
info.getSysfs_path(); // ....没弄清楚什么意思
不能保证所有版本系统和机型都适合,暂时只用LG P990 2.3.7 和华硕 平板 4.0.3进行过测试,都可以成功获取外置SD卡路径, 若此方法在你的机型或系统中无法获取相应路径,请回复,good luck!

有另一种方法可以得到外置SD卡路径:
[java] view plaincopyprint?
- System.getenv();
- Map<String, String> map = System.getenv();
-
-
-
- Set<String> set = map.keySet();
- Iterator<String> key = set.iterator();
- while(key.hasNext())
- Log.d("123", key.next());
-
- Collection<String> col = map.values();
- Iterator<String> val = col.iterator();
- while(val.hasNext())
- Log.d("123", val.next());
-
-
通过android 的api 可以获取系统自带的sdcard存储目录,但有些机器本身具有内置sdcard和外置TF卡,这样通过系统获取的路径只能是内置的,鉴于android的碎片化,rom不同,有些手机可能有两个存储目录mnt
mnt/sdcard/ mnt/extsdcard或者storage/sdcard0 storage/extsdcard
所以如果必须使用外置tf卡时可以通过反射方法获取路径名称
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
String mount = new String();
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.contains("secure"))
continue;
if (line.contains("asec"))
continue;
if (line.contains("fat")) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1)
{
mount = mount.concat("*" + columns[1] + "\n");
}
} else if (line.contains("fuse")) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
mount = mount.concat(columns[1] + "\n");
}
}
}
System.out.printf(mount);
} catch (FileNotFoundException e)
{ // TODO Auto-generated
catch block e.printStackTrace();
} catch (IOException e)
{ // TODO Auto-generated
catch block e.printStackTrace();
}