Android开发中如何获取外置TF卡的路径

来源:互联网 发布:皓月乒乓 淘宝店铺 编辑:程序博客网 时间:2024/04/27 21:15

Android获取外部SDcard的官方调用是
Environment.getExternalStorageDirectory()

但是根据不同机型,是有不同的结果的。
如果手机支持TF卡扩展,并且本身没有大容量存储,比如说HTC老款手机 G3之类的,那么获取到的是TF卡的路径。这是对的。
如果手机本身有大容量存储,不论手机是否支持TF卡扩展,比如现在HTC系列高端手机One X,或者920 butterfly系列,那么获取到的是大容量存储的路径。

也就是说,如果手机本身具有大容量存储,又支持TF卡扩展,通过这条命令是无法获取到TF卡信息的。
我查了半天,没有看到明确获取到扩展存储的API方法,如果有,请你告诉我,谢谢。(cst05001 at 狗妹儿)

那么怎么解决这个问题呢。网上有人说用/sdcard/extSdCard路径之类,这是不对的。因为不同的ROM,对外部存储的挂载点是会不一样的。比如三星国行,挂载点是/Storage/extSdCard 。

这是我从系统角度想到的解决办法,不用越狱,不用权限。大家可以参考:

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");}}}txtView.setText(mount);} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}


2 2
原创粉丝点击