当前位置: 代码迷 >> Android >> android 获取外置SD卡的途径(非挫版)
  详细解决方案

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

热度:53   发布时间:2016-04-28 07:17:45.0
android 获取外置SD卡的路径(非挫版)

转自:http://blog.csdn.net/bbmiku/article/details/7937745

?

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. [email protected]?
  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. [email protected]?
  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. [email protected]?
  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. [email protected]?
  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. [email protected]?
  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. //不同的机型获得的会有所不同,先试试!!
  相关解决方案