我有 android 4.1.2 的三星 Galaxy s3。我的内部手机内存命名为 sdcard0 和我的外部卡 extSdCard。
Environment.getExternalStorageDirectory()
所以上面返回sdcard0的路径,它是手机内部存储器
在这种情况下,要获得实际路径,您可以使用以下
String externalpath = new String();
String internalpath = new String();
public void getExternalMounts() {
Runtime runtime = Runtime.getRuntime();
try
{
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")) {//external card
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
externalpath = externalpath.concat("*" + columns[1] + "\n");
}
}
else if (line.contains("fuse")) {//internal storage
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
internalpath = internalpath.concat(columns[1] + "\n");
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("Path of sd card external............"+externalpath);
System.out.println("Path of internal memory............"+internalpath);
}
一旦你得到了路径。
File dir= new File(android.os.Environment.getExternalStorageDirectory());
//Instead of android.os.Environment.getExternalStorageDirectory() you can use internalpath or externalpath
然后打电话
walkdir(dir);
ArrayList<String> filepath= new ArrayList<String>();//contains list of all files ending with .xml
public void walkdir(File dir) {
String xmlPattern = ".xml";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(xmlPattern)){
//Do what ever u want
filepath.add( listFile[i].getAbsolutePath());
}
}
}
}
}