How to list additional external storage folders (mount points)?
By default, "external" storage on Android is presented as /sdcard folder. On some devices it's not SD card but internal memory.
Some devices allow attachment of additional SD card, or in case of Asus Transformer, also additional 2 USB flash discs.
Such added memory drive appears as some folder, which location depends on device manufacturer. On some devices it's inside /sdcard folder, on others it's elsewhere in other folder.
Now my question is, if there's some function to list all possible external storages besides standard /sdcard folder.
You can read /proc/mounts
to find out what is currently mounted.
Or you can read /etc/vold.conf
or /etc/vold.fstab
(it depends on version what config file is present). This files contains config for removable storages. At tablet default storage is not usually present in vold.
I agree with @Salw
Please see below code:
-------------end--------------
public static HashSet<String> getStorageSet(){
HashSet<String> storageSet = getStorageSet(new File("/system/etc/vold.fstab"), true);
storageSet.addAll(getStorageSet(new File("/proc/mounts"), false));
if (storageSet == null || storageSet.isEmpty()) {
storageSet = new HashSet<String>();
storageSet.add(Environment.getExternalStorageDirectory().getAbsolutePath());
}
return storageSet;
}
public static HashSet<String> getStorageSet(File file, boolean is_fstab_file) {
HashSet<String> storageSet = new HashSet<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = reader.readLine()) != null) {
HashSet<String> _storage = null;
if (is_fstab_file) {
_storage = parseVoldFile(line);
} else {
_storage = parseMountsFile(line);
}
if (_storage == null)
continue;
storageSet.addAll(_storage);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
reader = null;
}
/*
* set default external storage
*/
storageSet.add(Environment.getExternalStorageDirectory().getAbsolutePath());
return storageSet;
}
private static HashSet<String> parseMountsFile(String str) {
if (str == null)
return null;
if (str.length()==0)
return null;
if (str.startsWith("#"))
return null;
HashSet<String> storageSet = new HashSet<String>();
/*
* /dev/block/vold/179:19 /mnt/sdcard2 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
* /dev/block/vold/179:33 /mnt/sdcard vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1000,gid=1015,fmask=0002,dmask=0002,allow_utime=0020,codepage=cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
*/
Pattern patter = Pattern.compile("/dev/block/vold.*?(/mnt/.+?) vfat .*");
Matcher matcher = patter.matcher(str);
boolean b = matcher.find();
if (b) {
String _group = matcher.group(1);
storageSet.add(_group);
}
return storageSet;
}
private static HashSet<String> parseVoldFile(String str) {
if (str == null)
return null;
if (str.length()==0)
return null;
if (str.startsWith("#"))
return null;
HashSet<String> storageSet = new HashSet<String>();
/*
* dev_mount sdcard /mnt/sdcard auto /devices/platform/msm_sdcc.1/mmc_host
* dev_mount SdCard /mnt/sdcard/extStorages /mnt/sdcard/extStorages/SdCard auto sd /devices/platform/s3c-sdhci.2/mmc_host/mmc1
*/
Pattern patter1 = Pattern.compile("(/mnt/[^ ]+?)((?=[ ]+auto[ ]+)|(?=[ ]+(\\d*[ ]+)))");
/*
* dev_mount ins /mnt/emmc emmc /devices/platform/msm_sdcc.3/mmc_host
*/
Pattern patter2 = Pattern.compile("(/mnt/.+?)[ ]+");
Matcher matcher1 = patter1.matcher(str);
boolean b1 = matcher1.find();
if (b1) {
String _group = matcher1.group(1);
storageSet.add(_group);
}
Matcher matcher2 = patter2.matcher(str);
boolean b2 = matcher2.find();
if (!b1 && b2) {
String _group = matcher2.group(1);
storageSet.add(_group);
}
/*
* dev_mount ins /storage/emmc emmc /devices/sdi2/mmc_host/mmc0/mmc0:0001/block/mmcblk0/mmcblk0p
*/
Pattern patter3 = Pattern.compile("/.+?(?= )");
Matcher matcher3 = patter3.matcher(str);
boolean b3 = matcher3.find();
if (!b1 && !b2 && b3) {
String _group = matcher3.group(1);
storageSet.add(_group);
}
return storageSet;
}
-------------end--------------
You can call getStorageSet() method to get all storages. But you need check which are available.
Refernce
http://sapienmobile.com/?p=204
Find an external SD card location
http://renzhi.ca/2012/02/03/how-to-list-all-sd-cards-on-android/
You can only get the primary external storage by calling Environment.getExternalStorageDirectory()
.
There is currently no method that returns the secondary storages. Some manufacturers work around this with their own API though, e.g. Motorola.
Running mount
without any parameters should not take root privileges and should print out all the mounted file systems. Obviously it's up to you to parse the output and test across devices.
Relying on Linux tools is generally not a good idea on Android but I expect mount to be available everywhere..