问题
I am working on a app that can connect to a USB SD card reader. The problem is the USB path is not the same for all the phones. I know that in Samsung phones the USB path is "/storage/UsbDriveA/"
My question is how can I find the USB mount path for all phone devices?
thank you
回答1:
private String getAllStoragePath() {
String finalPath = "";
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("mount");
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
String line;
String[] pathArray = new String[4];
int i = 0;
BufferedReader br = new BufferedReader(inputStreamReader);
while ((line = br.readLine()) != null) {
String mount = "";
if (line.contains("secure"))
continue;
if (line.contains("asec"))
continue;
if (line.contains("fat")) {// TF card
String columns[] = line.split(" ");
if (columns.length > 1) {
mount = mount.concat(columns[1] + "/someFiles");
pathArray[i++] = mount;
// check directory inputStream exist or not
File dir = new File(mount);
if (dir.exists() && dir.isDirectory()) {
// do something here
finalPath = mount;
break;
}
}
}
}
for(String path:pathArray){
if(path!=null){
finalPath =finalPath + path +"\n";
}
}
} catch (Exception e) {
e.printStackTrace();
}
return finalPath;
}
来源:https://stackoverflow.com/questions/34513537/android-detect-usb-mount-point-path