欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-19 15:46:22
android获取内置sdcard跟外置sdcard路径 android获取内置sdcard跟外置sdcard路径.(测试过两个手机,亲测可用) 1.先得到外置sd...

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; 
} 

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

上一篇:

下一篇: