Pregunta

I've been scanning /etc/vold.fstab to get a list of external storage. It works fine until Android 4.3 that Google removed the file. I know that a unified /fstab.* file is now used but which is inaccessible without root.

So in Android 4.3, what should I do to get a list of external storage?

My code looks something like this. Now it includes both non-removable internal and removable external storage.

File voldFile = new File("/system/etc/vold.fstab");

fr = new FileReader(voldFile);
br = new BufferedReader(fr);
String line = br.readLine();
while (line != null) {
    if (line.startsWith("dev_mount")) {
        String[] tokens = line.split("\\s");
        File mountPoint = new File(tokens[2]);
        if (mountPoint.isDirectory() && mountPoint.canRead())
            list.add(tokens[2]);
    }
    line = br.readLine();
}
¿Fue útil?

Solución

I ended up scanning /proc/mounts output for currently mounted storage. Code similar to below.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    File voldFile = new File("/proc/mounts");

    fr = new FileReader(voldFile);
    br = new BufferedReader(fr);
    String line = br.readLine();
    while (line != null) {
        Log.d(TAG, line);
        if (line.startsWith("/")) {
            String[] tokens = line.split("\\s+");
            if ("vfat".equals(tokens[2])) {
                File mountPoint = new File(tokens[1]);
                if (!tokens[1].equals(defaultMount))
                    if (mountPoint.isDirectory() && mountPoint.canRead())
                        list.add(tokens[1]);
            }
        }
        line = br.readLine();
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top