0

I have tried almost all solutions to get list of storage paths including How can I get external SD card path for Android 4.0+?. But nothing returns the removable sd card path.

And finally I tried with "/mnt/". This returns all storage paths including some other paths also. But I want only storage paths. How could I achieve this?. Is there any way to detect whether the given path is storage path, or is there any better way to get all storage paths in Android.

Community
  • 1
  • 1
Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
  • As we do not know what you tried it's hard to suggest something new. – greenapps Sep 27 '16 at 12:54
  • On which Android versiion should your code run? – greenapps Sep 27 '16 at 12:56
  • @greenapps I have tried all solutions from http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0/13648873#13648873. And my code runs from KitKat. – Gunaseelan Sep 27 '16 at 12:57
  • Since you cannot use "the removable sd card path" on API Level 19+ devices, why do you want it? You do not have arbitrary read/write access to removable storage on Android 4.4+ devices. – CommonsWare Sep 27 '16 at 12:58
  • I will not read your link. And there are so few possibilities to discover such a path that i don't undersatnd why you don't mention them. – greenapps Sep 27 '16 at 13:03
  • @greenapps I am trying to make one file browser app... So I have to get removable sd cards... Because some devices has the sd card.. I have tested this code in two devices, one marshmallow(Lenova A1070 with SD card) and nougat (Nexus 5X without sd card). – Gunaseelan Sep 27 '16 at 13:04
  • `You do not have arbitrary read/write access to removable storage`. There is still arbitrary read access. – greenapps Sep 27 '16 at 13:05
  • @greenapps But by using ES File explorer, we can move/edit/delete files between Internal SD card and removable sd card. I want to make similar to that. – Gunaseelan Sep 27 '16 at 13:06
  • `I have tested this code`. I see no code. And certainly on Marshmellow it's easy to determine the sd card. But as i do not know what you tried... – greenapps Sep 27 '16 at 13:07

1 Answers1

1

You can use this class for getting all available storage on Android Phone. This code is taken from stackoverflow.com but contains some error. I remove the error and this code tested in Android 6 and Android 7.

public class StorageUtils {

    private static final String TAG = "StorageUtils";

    public static class StorageInfo {

        public final String path;
        public final boolean readonly;
        public final boolean removable;
        public final int number;
        StorageInfo(String path, boolean readonly, boolean removable, int number) {
            this.path = path;
            this.readonly = readonly;
            this.removable = removable;
            this.number = number;
        }

        public String getDisplayName() {
            StringBuilder res = new StringBuilder();
            if (!removable) {
                res.append("Internal Storage");
            } else if (number > 1) {
                res.append("SD card " + number);
            } else {
                res.append("SD card");
            }
            if (readonly) {
                res.append(" (Read only)");
            }
            return res.toString();
        }
    }

    public static List<StorageInfo> getStorageList() {

        List<StorageInfo> list = new ArrayList<StorageInfo>();
        String def_path = Environment.getExternalStorageDirectory().getPath();
        boolean def_path_removable = Environment.isExternalStorageRemovable();
        String def_path_state = Environment.getExternalStorageState();
        boolean def_path_available = def_path_state.equals(Environment.MEDIA_MOUNTED)
                || def_path_state.equals(Environment.MEDIA_MOUNTED_READ_ONLY);
        boolean def_path_readonly = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY);

        HashSet<String> paths = new HashSet<String>();
        int cur_removable_number = 1;

        if (def_path_available) {
            paths.add(def_path);
            list.add(0, new StorageInfo(def_path, def_path_readonly, def_path_removable, def_path_removable ? cur_removable_number++ : -1));
        }

        BufferedReader buf_reader = null;
        try {
            buf_reader = new BufferedReader(new FileReader("/proc/mounts"));
            String line;
            while ((line = buf_reader.readLine()) != null) {

                if (line.contains("vfat") || line.contains("/mnt") || line.contains("sdcardfs")) {
                    Log.d(TAG, line);
                    StringTokenizer tokens = new StringTokenizer(line, " ");
                    String mPath = tokens.nextToken(); //device
                    String mount_point = tokens.nextToken(); //mount point
                    if (paths.contains(mount_point)) {
                        continue;
                    }
                    String unused = tokens.nextToken(); //file system
                    List<String> flags = Arrays.asList(tokens.nextToken().split(",")); //flags
                    boolean readonly = flags.contains("ro");

                         if (!line.contains("/mnt/secure")
                                && !line.contains("/mnt/asec")
                                && !line.contains("/mnt/obb")
                                && !line.contains("/dev/mapper")
                                && !line.contains("tmpfs")) {
                             File testFile  = new File(mount_point);
                             if(testFile.isDirectory() && testFile.listFiles() != null) {
                                 paths.add(mount_point);
                                 list.add(new StorageInfo(mount_point, readonly, true, cur_removable_number++));
                             }
                        }
                }
            }

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (buf_reader != null) {
                try {
                    buf_reader.close();
                } catch (IOException ex) {}
            }
        }
        return list;
    }
}

Hope this help.

masoomyf
  • 685
  • 8
  • 15