3

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++);
}
xandi1987
  • 519
  • 1
  • 5
  • 17
  • You better change external storage to removable storage. As there is internal, external and removable. The two first ones belong to the device and cannot be removed. – greenapps Jan 08 '16 at 19:00
  • True, this would be much better wording. Do you have any suggestion for the outcome? – xandi1987 Jan 08 '16 at 19:04
  • 2
    Try this: https://gist.github.com/jaredrummler/49ec70139d8148244e4d – Jared Rummler Jan 09 '16 at 00:45
  • This seems to work perfect. The only problem I have still is that I cannot write files there. I have put the but I cannot write directories or files (permission denied error). Can you help me one more time please? – xandi1987 Jan 09 '16 at 09:16
  • Maybe I provide a little more background of my idea: I want to offer an automatic backup. So the user defines the backup path via setting (e.g. on SD or Usb or later even cloud) and the app automatically saves data to the path (so without further user actions). After a little more research I found Storage Access Framework. Maybe this is a better idea to use? I just made little experiments but it seems promising. What would you say? – xandi1987 Jan 09 '16 at 11:37

1 Answers1

0

Finally for my Question Jared Rummler's answer covers exactly what I asked for. The only problem was that I didn't get any permission on this folders. So what I actually did was to use the Storage Access Framework (http://developer.android.com/guide/topics/providers/document-provider.html) which covers exactly my needs. Thank you very much Rummler, I really would like to mark your answer as the correct on. Please provide your commend as the answer.

xandi1987
  • 519
  • 1
  • 5
  • 17