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 infoif (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;}}@Overridepublic DevInfo getInternalInfo() {return getInfo(DEV_INTERNAL);}@Overridepublic DevInfo getExternalInfo() {return getInfo(DEV_EXTERNAL);}}interface IDev {DevInfo getInternalInfo();DevInfo getExternalInfo();}



使用方法:

Dev_MountInfo dev = Dev_MountInfo.getInstance();DevInfo info = dev.getInternalInfo();//Internal SD Card Informationsinfo = 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卡路径:

System.getenv(); // 返回的是一个mapMap<String, String> map = System.getenv();//遍历出来可以看到最后一项是外置SD卡路径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());//不同的机型获得的会有所不同,先试试!!


更多相关文章

  1. unity通过android adb查看真机日志
  2. Android查看数据库方法及工具
  3. Android生成二维码--拍照或从相册选取图片
  4. Android(安卓)app如何正确读写系统sys设备节点
  5. android 编译系统阅读与分析(1)-envsetup.sh (editing....)
  6. Android(安卓)2D画图类Path精炼详解
  7. Android开发实践:如何设置NDK的编译选项
  8. Android中绘制简单几何图形和路径Path
  9. android:windowIsTranslucent &分享回调 引发的血案

随机推荐

  1. 【Android(安卓)界面效果22】Android的Ta
  2. Android音频编解码和混音实现
  3. 做了一个半成品的开源小工具:pdf->word,没
  4. 【Android】自定义View
  5. 如何在 Android(安卓)程序中禁止屏幕旋转
  6. 面试java不知道Vector?恐怕要重在参与了
  7. 操作系统面试题汇总(找工作必备)
  8. 使用Android中sqlite表的全文检索
  9. Babel快速指南
  10. [Android]笔记10-单选按钮和复选框的功能