获取外置卡路径

public String getExtralCardPath()
{
try
{
FileReader fr = new FileReader("/system/etc/vold.fstab");
BufferedReader br = new BufferedReader(fr);
while (br.ready())
{
String line = br.readLine().trim();
if(line.startsWith("dev_mount"))
{
String[] arrStr = line.split(" ");
if(arrStr.length >= 5 && arrStr[0].equals("dev_mount")
&& arrStr[1].equals("sdcard"))
{
br.close();
if(new File(arrStr[2]).exists())
{
File f = new File(arrStr[2]);
if(f.canWrite())
{
return arrStr[2];
}
}
}
}
}
} catch (Exception e)
{
String path = "";
Map<String, String> map = System.getenv();
if(map.containsKey("SECONDARY_STORAGE"))
{
path = map.get("SECONDARY_STORAGE").split(":")[0];
} else if(map.containsKey("EXTERNAL_STORAGE"))
{
path = map.get("EXTERNAL_STORAGE");
}
if(new File(path).exists())
{
File f = new File(path);
if(f.canWrite())
{
return path;
}
}
}
String sdPath = Environment.getExternalStorageDirectory().getPath();
if(new File(sdPath).exists())
{
return sdPath;
} else
{
File SDfiles = Environment.getExternalStorageDirectory(); // SDCards(内置和外置SDCard)
if(SDfiles != null)
{
File parentFile = SDfiles.getParentFile();
// 列出该父目录下的所有路径
File[] listFiles = parentFile.listFiles();
// 如果子路径可以写 就是拓展卡(包含内置的和外置的)
for (int i = 0; i < listFiles.length; i++)
{
if(listFiles[i].canWrite()
&& new File(listFiles[i].getPath()).exists())
{
return listFiles[i].getPath();
}
}
}
}
return null;
}

你可能感兴趣的:(获取外置卡路径)