android 获取外置SD卡的路径(非挫版)

From:http://www.verydemo.com/demo_c131_i146427.html

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

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

Dev_MountInfo.class

[java] view plaincopyprint?
  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. interface IDev { 
  163.     DevInfo getInternalInfo(); 
  164.  
  165.     DevInfo getExternalInfo(); 
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 plaincopyprint?
  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(); // ....没弄清楚什么意思 
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?

  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. //不同的机型获得的会有所不同,先试试!! 

 

 

 

通过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();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值