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

时间:2011-09-16 21:39:05

标签: android

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

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

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

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

6 个答案:

答案 0 :(得分:7)

您可以阅读/proc/mounts以了解当前安装的内容。

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

答案 1 :(得分: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--------------

您可以调用getStorageSet()方法来获取所有存储空间。但是你需要检查哪些是可用的。
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/

答案 2 :(得分:3)

您只能通过致电Environment.getExternalStorageDirectory()获取主外部存储空间。

目前没有方法可以返回辅助存储。一些制造商使用他们自己的API解决这个问题,例如, Motorola

答案 3 :(得分:2)

不带任何参数运行mount不应具有root权限,并应打印出所有已安装的文件系统。显然,您需要解析输出并跨设备进行测试。

在Android上依赖Linux工具通常不是一个好主意,但我希望mount可以随处使用..

答案 4 :(得分:0)

我找到了一个使用/ proc / mount来返回所有已安装存储的代码:

How can I get the list of mounted external storage of android device

我已经在4种不同的设备上测试了它,它就像一个魅力。

答案 5 :(得分:0)

我现在这个话题已经过时但这可能有所帮助。 你应该使用这种方法。

System.getenv();

请参阅项目Environment3以访问连接到您设备的所有存储空间。

https://github.com/omidfaraji/Environment3