问题
Is there any way of identifying if there are two sd-cards in a device??
Edit:
I have found that at the moment, there is no way of distinguishing between internal storage and true external SD card. In some devices like Samsung Galaxy Tab (7 inches), the system takes the internal storage (usually 16gb) to be as an external storage. Unfortunately there is no way to distinguish between internal storage and secondar/external/sd-card storage. If someone thinks it is possible (for honeycomb and previous versions), write here and I'll prove it.
回答1:
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.
回答2:
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);
}
来源:https://stackoverflow.com/questions/7044545/android-how-to-detect-dual-sd-card