Android:如何检测双SD卡

时间:2011-08-12 18:19:06

标签: android sd-card

有没有办法确定设备中是否有两张SD卡?

编辑:

我发现目前无法区分内部存储和真正的外部SD卡。在三星Galaxy Tab(7英寸)等设备中,系统将内部存储(通常为16GB)作为外部存储。遗憾的是,没有办法区分内部存储和第二/外部/ SD卡存储。如果有人认为这是可能的(对于蜂窝和以前的版本),写在这里,我会证明它。

2 个答案:

答案 0 :(得分:1)

我不相信有办法检查双SD卡,但有些设备确实有2种类型的外部存储。例如,我知道在某些摩托罗拉设备上,使用/sdcard-ext访问内部辅助存储。您可以检查此目录是否存在(我知道其他具有辅助存储的设备也使用-ext追加)并做出相应的反应。

答案 1 :(得分:0)

有些设备同时具有模拟SD和物理SD。 (例如Sony Xperia Z)。 它不会暴露物理SD卡,因为像getExternalFilesDir(null)这样的方法将返回模拟的SD卡。 我使用以下代码获取物理SD的目录。 该调用返回所有挂载点和在线SD卡。你必须弄清楚哪个挂载点是指OFFLINE SD卡(如果有的话),但大多数时候你只对ONLINE SD卡感兴趣。

        public static boolean getMountPointsAndOnlineSDCardDirectories(ArrayList<String> mountPoints, ArrayList<String> sdCardsOnline)
        {
            boolean ok = true;

            mountPoints.clear();
            sdCardsOnline.clear();

            try
            {                   
                // File that contains the filesystems to be mounted at system startup
                FileInputStream fs = new FileInputStream("/etc/vold.fstab");
                DataInputStream in = new DataInputStream(fs);
                BufferedReader  br = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = br.readLine()) != null) 
                {
                    // Skip comments and empty lines
                    line = line.trim();
                    if ((line.length() == 0) || (line.startsWith("#"))) continue;

                    // Fields are separated by whitespace
                    String[] parts = line.split("\\s+");
                    if (parts.length >= 3)
                    {
                        // Add mountpoint
                        mountPoints.add(parts[2]);
                    }
                }

                in.close();
            }
            catch (Exception e)
            {
                ok = false;
                e.printStackTrace();
            }

            try
            {                   

                // Pseudo file that holds the CURRENTLY mounted filesystems
                FileInputStream fs = new FileInputStream("//proc/mounts");
                DataInputStream in = new DataInputStream(fs);
                BufferedReader  br = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = br.readLine()) != null) 
                {
                    // A sdcard would typically contain these...
                    if (line.toLowerCase().contains("dirsync") && line.toLowerCase().contains("fmask"))
                    {
                        String[] parts = line.split("\\s+");
                        sdCardsOnline.add(parts[1]);

                    }
                }

                //Close the stream
                in.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                ok = false;
            }

            return (ok);
        }
相关问题