Android: How to detect Dual sd-card [duplicate]

佐手、 提交于 2019-12-01 00:20:52

I don't believe there is a way to check for dual sd-cards, but some devices do have 2 types of external storage. For example, I know on some motorola devices, the internal secondary storage is accessed with /sdcard-ext. You could check to see if this directory exists (I know that other devices with secondary storage also use the -ext append) and react accordingly.

There are devices that have both an emulated and a physical SD. (eg Sony Xperia Z). It won't expose the physical SD card because methods like getExternalFilesDir(null) will return the emulated SD card. I use the following code to get the directory for the physical SD. The call returns all mountpoints and the online SD cards. You will have to figure out which mountpoint refers to a OFFLINE SD card (if any), but most of the time you are only interested in ONLINE SD cards.

        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);
        }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!