博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取android系统外置存储卡路径的方法
阅读量:6955 次
发布时间:2019-06-27

本文共 1631 字,大约阅读时间需要 5 分钟。

hot3.png

    android系统可通过Environment.getExternalStorageDirectory()获取存储卡的路径,但是现在有很多手机内置有一个存储空间,同时还支持外置sd卡插入,这样通过Environment.getExternalStorageDirectory()方法获取到的就是内置存储卡的位置,需要获取外置存储卡的路径就比较麻烦,这里借鉴网上的代码,稍作修改,在已有的手机上做了测试,效果还可以,当然也许还有其他的一些奇葩机型没有覆盖到,如果您看到这个文章后测试有问题,欢迎留言跟我交流,大家互相学习进步,关键代码如下:

 

 

//获取外置存储卡的根路径,如果没有外置存储卡,则返回null public String getPath2() {  String sdcard_path = null;  String sd_default = Environment.getExternalStorageDirectory()    .getAbsolutePath();  if (sd_default.endsWith("/")) {   sd_default = sd_default.substring(0, sd_default.length() - 1);  }  // 得到路径  try {   Runtime runtime = Runtime.getRuntime();   Process proc = runtime.exec("mount");   InputStream is = proc.getInputStream();   InputStreamReader isr = new InputStreamReader(is);   String line;   BufferedReader br = new BufferedReader(isr);   while ((line = br.readLine()) != null) {    if (line.contains("secure"))     continue;    if (line.contains("asec"))     continue;    if (line.contains("fat") && line.contains("/mnt/")) {     String columns[] = line.split(" ");     if (columns != null && columns.length > 1) {      if (sd_default.trim().equals(columns[1].trim())) {       continue;      }      sdcard_path = columns[1];     }    } else if (line.contains("fuse") && line.contains("/mnt/")) {     String columns[] = line.split(" ");     if (columns != null && columns.length > 1) {      if (sd_default.trim().equals(columns[1].trim())) {       continue;      }      sdcard_path = columns[1];     }    }   }  } catch (Exception e) {   // TODO Auto-generated catch block   e.printStackTrace();  }  return sdcard_path; }

转载于:https://my.oschina.net/u/269663/blog/209263

你可能感兴趣的文章
虚拟网卡实现虚拟机与本机互访
查看>>
我的友情链接
查看>>
Spark-GraphxAPI学习笔记
查看>>
异次元软件
查看>>
Android 冷启动时间优化
查看>>
hadoop报JAVA_HOME is not set暂时解决办法
查看>>
jQuery笔记
查看>>
圆——高精无理数的处理
查看>>
[UOJ348]州区划分
查看>>
Android -- RecyclerView实现顶部吸附效果
查看>>
微信video标签全屏无法退出bug
查看>>
[转]PostgreSQL 中文资料汇总
查看>>
那些被疯狂追求的女孩,后来怎么样了?
查看>>
(转载)Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)...
查看>>
内置函数
查看>>
mysql之触发器
查看>>
C 入门 第七节 结构体
查看>>
linux下安装svn
查看>>
Flash Builder快捷键
查看>>
js通过Image和canvas获取图片的base64格式的字符串(只能接受服务器上的图片,不支持本地图片直接转化为base64,因为js没有系统io的权限,js只能操作dom)...
查看>>