当前位置: 移动技术网 > 移动技术>移动开发>Android > Android获取内置sdcard跟外置sdcard路径

Android获取内置sdcard跟外置sdcard路径

2019年07月24日  | 移动技术网移动技术  | 我要评论

android获取内置sdcard跟外置sdcard路径

android获取内置sdcard跟外置sdcard路径.(测试过两个手机,亲测可用)

1.先得到外置sdcard路径,这个接口是系统提供的标准接口.
2.得到上一级文件夹目录
3.得到该目录的所有文件夹,根据判断得到内置sdcard跟外置sdcard。
4.程序运行的时候记得给程序添加读取sdcard的权限哦.

/** 
 * 得到sdcard的路径 
 * @return 返回一个字符串数组  下标0:内置sdcard  下标1:外置sdcard 
 */ 
public static string[] getsdcardpath(){ 
  string[] sdcardpath=new string[2]; 
  file sdfile=environment.getexternalstoragedirectory(); 
  file[] files=sdfile.getparentfile().listfiles(); 
  for(file file:files){ 
    if(file.getabsolutepath().equals(sdfile.getabsolutepath())){//外置 
      sdcardpath[1]=sdfile.getabsolutepath(); 
    }else if(file.getabsolutepath().contains("sdcard")){//得到内置sdcard 
      sdcardpath[0]=file.getabsolutepath(); 
    } 
  } 
  return sdcardpath; 
} 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网