由于现在的大部分Android手机自带了SD卡,所以用
Environment.getExternalStorageDirectory().toString();
一般获取的都是机身集成的SD卡路径,但是内置的SD卡存储较小,所以还需要获取外置的SD卡,但是由于Android的开源,不同的手机厂商对Android的外置卡的名称做了修改,
所以一下代码 不敢确定支持所有的Android系统都能够获取外置SD卡的正确路径。
本人使用三星P-600,P-601,T-320和联想TAB 2 A10-70F做了测试,获取的外置SD卡的路径均无问题。
private static StorageManager manager;
private static Method method;
public static String getExternalSdCard(Context context) {
manager = (StorageManager) context.getSystemService(Activity.STORAGE_SERVICE);
try {
method = manager.getClass().getMethod("getVolumePaths");
String[] paths = (String[]) method.invoke(manager);
String insideSdCard = Environment.getExternalStorageDirectory().toString();
if (insideSdCard.equals(paths[0])) {
File externalFile = new File(paths[1]);
if (!externalFile.exists()) {
BaseUtil.setAlertDialog(context, "外部存储卡不存在!");
return null;
}
return paths[1];
} else {
BaseUtil.setAlertDialog(context, "该机型无法获取外部存储!");
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}