I am already looking several days for a clear answer, but I am still not satisfying with the solutions which I have found. So I am asking once more on stackoverflow:
Is there any efficient way to get all mounted external storage on an Android device? I know that managing external storage always depends on the producer of the device. But I don't wonna publish my app and wait for incoming "bugs".
So I have produced my code on "trial and error" based on two different devices (Samsung and Acer).
The code in attach makes the best outcome for my needs (but still not perfect, because the Acer device has the SD card shown in both "mnt" and "storage" and further more with different names).
As I know that this code is not a professional way to manage that, please help me with better solutions.
public static List<StorageInfo> getStorageList() {
List<StorageInfo> list = new ArrayList<>();
List<File> typicallFolders = new ArrayList<>();
typicallFolders.add(new File("/storage"));
typicallFolders.add(new File("/mnt"));
int internalNumber = 1;
HashSet<String> knownPaths = new HashSet<>();
for(File fs:typicallFolders) {
try {
if(!fs.exists())
continue;
for (File f : fs.listFiles()) {
File finalPath = getexternalStorage(f, 1);
if (finalPath != null && !knownPaths.contains(finalPath.toString()) && finalPath.canWrite()) {
list.add(new StorageInfo(finalPath.toString(), true, true, internalNumber++));
knownPaths.add(finalPath.toString());
}
}
} catch (Exception e) {
//Catch exception if any
e.printStackTrace();
}
}
return list;
}
private static File getexternalStorage(File path,int depth){
if(Environment.isExternalStorageRemovable(path))
return path;
File[] subFolders = path.listFiles();
if(depth == 3 || subFolders.length != 1){
return null;
}
return getexternalStorage(subFolders[0],depth++);
}