如何列出其他外部存储文件夹(挂载点)?

Poi*_*ull 4 android

默认情况下,Android上的"外部"存储显示为/ sdcard文件夹.在某些设备上,它不是SD卡而是内部存储器.

某些设备允许附加额外的SD卡,或者在Asus Transformer的情况下,还可以附加2个USB闪存盘.

这样添加的内存驱动器显示为某个文件夹,该位置取决于设备制造商.在某些设备上,它位于/ sdcard文件夹中,而在其他设备上则位于其他文件夹中的其他位置.

现在我的问题是,如果有一些功能可以列出除标准/ sdcard文件夹之外的所有可能的外部存储器.

Sal*_*alw 7

您可以阅读/proc/mounts以找出当前安装的内容.

或者您可以阅读/etc/vold.conf/etc/vold.fstab(它取决于版本存在的配置文件).此文件包含可移动存储的配置.平板电脑默认存储通常不会出现在vold中.

  • 此答案使用此方法提供了完整的解决方案:http://stackoverflow.com/a/19982338/747412. (2认同)

boi*_*ter 6

我同意@Salw
请看下面的代码:

-------------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--------------
Run Code Online (Sandbox Code Playgroud)

您可以调用getStorageSet()方法来获取所有存储.但是你需要检查哪些是可用的.
参考
http://sapienmobile.com/?p=204
查找外部SD卡位置
http://renzhi.ca/2012/02/03/how-to-list-all-sd-cards-on-android/


归档时间:

查看次数:

16177 次

最近记录:

10 年,2 月 前