2011-08-12 43 views
2

有什么方法可以识别设备中是否有两个SD卡?Android:如何检测双SD卡

编辑:

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

回答

1

我不认为有一种方法来检查双sd卡,但有些设备确实有两种类型的外部存储。例如,我知道在一些摩托罗拉设备上,内部辅助存储设备可通过/sdcard-ext进行访问。你可以检查这个目录是否存在(我知道其他带有辅助存储的设备也可以使用-ext追加)并作出相应的反应。

+0

是的,你是对的。有些设备认为内部存储为SD卡。你可以详细说明如何区分被视为SD卡的内部存储和实际的外部SD卡。 – Farhan

+1

查看'Environment.isExternalStorageEmulated()'和'Environment.isExternalStorageRemovable()' - 请参阅http://developer.android.com/reference/android/os/Environment.html – 2011-08-12 18:56:47

0

有设备既有仿真也有物理SD。 (如Sony Xperia Z)。 它不会公开物理SD卡,因为像getExternalFilesDir(null)这样的方法会返回模拟SD卡。 我使用下面的代码来获取物理SD的目录。 该调用返回所有挂载点和在线SD卡。你必须弄清楚哪一个挂载点是指一个离线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); 
     }