Android获取外置sdcard路径

android  未提供获取外置SD储存卡的相应函数或方法,但我们可以自己写一个。当前只能用Environment.getExternalStorageDirectory()获取内置的SD卡路径,因为不同机型的系统SD卡的路径不相同,但是我们仍然可以有方法去获得外置SD卡的路径,  内置和外置SD卡的信息存在system/etc/vold.fstab 里面,我们可以从这里获得外置SD卡的路径,  这里面的内容就算在设备非ROOT的情况下也是可读的,所以这个方法值得一试:

本人写好了一个类,可供直接使用或参考:

Dev_MountInfo.class

 

[java] view plaincopy


  1. import java.io.BufferedReader;  

  2. import java.io.File;  

  3. import java.io.FileReader;  

  4. import java.io.IOException;  

  5. import java.util.ArrayList;  

  6.   

  7. import com.snuabar.getmounteddevices.Dev_MountInfo.DevInfo;  

  8.   

  9. import android.os.Environment;  

  10.   

  11. public class Dev_MountInfo implements IDev {  

  12.     /** 

  13.      * *** 

  14.      */  

  15.     public final String HEAD = "dev_mount";  

  16.     public final String LABEL = "<label>";  

  17.     public final String MOUNT_POINT = "<mount_point>";  

  18.     public final String PATH = "<part>";  

  19.     public final String SYSFS_PATH = "<sysfs_path1...>";  

  20.   

  21.     /** 

  22.      * Label for the volume 

  23.      */  

  24.     private final int NLABEL = 1;  

  25.     /** 

  26.      * Partition 

  27.      */  

  28.     private final int NPATH = 2;  

  29.     /** 

  30.      * Where the volume will be mounted 

  31.      */  

  32.     private final int NMOUNT_POINT = 3;  

  33.     private final int NSYSFS_PATH = 4;  

  34.   

  35.     private final int DEV_INTERNAL = 0;  

  36.     private final int DEV_EXTERNAL = 1;  

  37.   

  38.     private ArrayList<String> cache = new ArrayList<String>();  

  39.   

  40.     private static Dev_MountInfo dev;  

  41.     private DevInfo info;  

  42.   

  43.     private final File VOLD_FSTAB = new File(Environment.getRootDirectory()  

  44.             .getAbsoluteFile()  

  45.             + File.separator  

  46.             + "etc"  

  47.             + File.separator  

  48.             + "vold.fstab");  

  49.   

  50.     public static Dev_MountInfo getInstance() {  

  51.         if (null == dev)  

  52.             dev = new Dev_MountInfo();  

  53.         return dev;  

  54.     }  

  55.   

  56.     private DevInfo getInfo(final int device) {  

  57.         // for(String str:cache)  

  58.         // System.out.println(str);  

  59.   

  60.         if (null == info)  

  61.             info = new DevInfo();  

  62.   

  63.         try {  

  64.             initVoldFstabToCache();  

  65.         } catch (IOException e) {  

  66.             e.printStackTrace();  

  67.         }  

  68.   

  69.         if (device >= cache.size())  

  70.             return null;  

  71.         String[] sinfo = cache.get(device).split(" ");  

  72.   

  73.         info.setLabel(sinfo[NLABEL]);  

  74.         info.setMount_point(sinfo[NMOUNT_POINT]);  

  75.         info.setPath(sinfo[NPATH]);  

  76.         info.setSysfs_path(sinfo[NSYSFS_PATH]);  

  77.   

  78.         return info;  

  79.     }  

  80.   

  81.     /** 

  82.      * init the words into the cache array 

  83.      * @throws IOException 

  84.      */  

  85.     private void initVoldFstabToCache() throws IOException {  

  86.         cache.clear();  

  87.         BufferedReader br = new BufferedReader(new FileReader(VOLD_FSTAB));  

  88.         String tmp = null;  

  89.         while ((tmp = br.readLine()) != null) {  

  90.             // the words startsWith "dev_mount" are the SD info  

  91.             if (tmp.startsWith(HEAD)) {  

  92.                 cache.add(tmp);  

  93.             }  

  94.         }  

  95.         br.close();  

  96.         cache.trimToSize();  

  97.     }  

  98.   

  99.     public class DevInfo {  

  100.         private String label, mount_point, path, sysfs_path;  

  101.   

  102.         /** 

  103.          * return the label name of the SD card 

  104.          * @return  

  105.          */  

  106.         public String getLabel() {  

  107.             return label;  

  108.         }  

  109.   

  110.         private void setLabel(String label) {  

  111.             this.label = label;  

  112.         }  

  113.   

  114.         /** 

  115.          * the mount point of the SD card 

  116.          * @return  

  117.          */  

  118.         public String getMount_point() {  

  119.             return mount_point;  

  120.         }  

  121.   

  122.         private void setMount_point(String mount_point) {  

  123.             this.mount_point = mount_point;  

  124.         }  

  125.   

  126.         /** 

  127.          * SD mount path 

  128.          * @return  

  129.          */  

  130.         public String getPath() {  

  131.             return path;  

  132.         }  

  133.   

  134.         private void setPath(String path) {  

  135.             this.path = path;  

  136.         }  

  137.   

  138.         /** 

  139.          * "unknow" 

  140.          * @return  

  141.          */  

  142.         public String getSysfs_path() {  

  143.             return sysfs_path;  

  144.         }  

  145.   

  146.         private void setSysfs_path(String sysfs_path) {  

  147.             this.sysfs_path = sysfs_path;  

  148.         }  

  149.   

  150.     }  

  151.   

  152.     @Override  

  153.     public DevInfo getInternalInfo() {  

  154.         return getInfo(DEV_INTERNAL);  

  155.     }  

  156.   

  157.     @Override  

  158.     public DevInfo getExternalInfo() {  

  159.         return getInfo(DEV_EXTERNAL);  

  160.     }  

  161. }  

  162.   

  163. interface IDev {  

  164.     DevInfo getInternalInfo();  

  165.   

  166.     DevInfo getExternalInfo();  

  167. }  


 


使用方法:

[java] view plaincopy


  1. Dev_MountInfo dev = Dev_MountInfo.getInstance();  

  2. DevInfo info = dev.getInternalInfo();//Internal SD Card Informations  

  3. info = dev.getExternalInfo();//External SD Card Informations  

  4.   

  5. //   Methods:  

  6. info.getLabel(); // SD 卡的名称  

  7. info.getMount_point();//SD 卡挂载点  

  8. info.getPath(); //SD 卡路径  

  9. info.getSysfs_path(); // ....没弄清楚什么意思  

 

不能保证所有版本系统和机型都适合,暂时只用LG P990 2.3.7  和华硕 平板 4.0.3进行过测试,都可以成功获取外置SD卡路径,  若此方法在你的机型或系统中无法获取相应路径,请回复,good luck!


 

有另一种方法可以得到外置SD卡路径:

[java] view plaincopy


  1. System.getenv(); // 返回的是一个map  

  2. Map<String, String> map = System.getenv();  

  3.   

  4. //遍历出来可以看到最后一项是外置SD卡路径  

  5.   

  6. Set<String> set = map.keySet();  

  7.         Iterator<String> key = set.iterator();  

  8.         while(key.hasNext())  

  9.             Log.d("123", key.next());  

  10.           

  11.         Collection<String> col = map.values();  

  12.         Iterator<String> val = col.iterator();  

  13.         while(val.hasNext())  

  14.             Log.d("123", val.next());  

  15.   

  16. //不同的机型获得的会有所不同,先试试!!  


 

Baidu Button BEGIN







Baidu Button END 192.168.100.33

  • 下一篇
    关于输入法的显示与隐藏的问题

Baidu Button BEGIN Baidu Button END

主题推荐

android

arraylist

interface

iterator

methods

猜你在找

AndroidAlertDialogBuilder创建常见对话框

Android Selector 与 Shape 基本用法

超漂亮的仿腾讯弹出层效果兼容主流浏览器

程序猿的克星

Android提高篇之自定义dialog实现processDialog正在加载效果使用Animation实现图片旋转

Android Service 详解四开始停止service

ExtJs grid批量处理

我的Android进阶之旅经典的大牛博客推荐

Android入门之Spinner纯java

准备好了么? 

         !

更多职位尽在 CSDN JOB

Android高级开发工程师

上海欢校信息科技有限公司

|

10-20K/月

我要跳槽

诚聘【android高级开发工程师】

畅捷通信息技术股份有限公司

|

15-25K/月

我要跳槽

互联网证券业务组_高级Android研发工程师

百度在线网络技术(北京)有限公司

|

15-30K/月

我要跳槽

Android/iOS终端开发工程师

腾讯科技(深圳)有限公司.

|

10-20K/月

我要跳槽

AdForward Begin: 博客频道内容页下方Banner




AdForward End

查看评论

5楼 pcpanchen 2014-07-09 11:54发表 [回复]


在三星s7562中,用“另外一种方法”得到的倒数第二项为外置sd卡的路径。

4楼 ssjcfiaw911 2014-05-12 09:17发表 [回复]


Thx,华为3c能够获取到外置sd卡

3楼 mars0920 2013-10-09 13:42发表 [回复]


你这方法在三星s3手机上测试就不行

Re: u012500046 2014-02-09 12:51发表 [回复]


不同厂家机型的配置文件不一样的,我的gt-i8852的外置内存卡就在0位置,你查看vold.fstab文件,里面有详细的配置信息

2楼 wujxiaoz 2013-08-21 10:28发表 [回复] [引用] [举报]


[java] view plaincopy


  1. [工具类一下]  

  2. /** 

  3.  * 多个SD卡时 取外置SD卡<br> 

  4.  * 参考:http://blog.csdn.net/bbmiku/article/details/7937745<br> 

  5.  * @return 

  6.  */  

  7. public static String getExternalStorageDirectory() {  

  8.     Map<String, String> map = System.getenv();  

  9.     String[] values = new String[map.values().size()];  

  10.     map.values().toArray(values);  

  11.     String path = values[values.length - 1];    //外置SD卡的路径  

  12.     if (path.startsWith("/mnt/") && !Environment.getExternalStorageDirectory().getAbsolutePath().equals(path)){  

  13.         return path;  

  14.     }else{  

  15.         return null;  

  16.     }  

  17. }  


你可能感兴趣的:(android,sdcard)