手机或平板,有内置SD卡和拓展的外置SD卡:
内置的路径为:
String sdcard_Path = Environment.getExternalStorageDirectory().getName();
外置的路径有点麻烦:
/**
* 外置SD卡的路径:exSd_Path
*/
public static String getDefaultExternalStoragePath() {
String exSd_Path = "";
String [] exPaths = new StorageList(MainActivity.contexts).getVolumePaths();
if (exPaths.length > 1) {
exSd_Path = exPaths[1];
}
return exSd_Path;
}
/*/mnt/sdcard
/mnt/extsd
/mnt/usbhost1*/
public class StorageList {
private Context mContext;
private Method method;
private StorageManager storageManager;
@SuppressLint("InlinedApi")
public StorageList(Context contexts) {
mContext = contexts;
if (mContext != null) {
storageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
try {
method = storageManager.getClass().getMethod("getVolumePaths", new Class[0]);
} catch (Exception e) {
}
}
}
public String[] getVolumePaths() {
String[] paths = null;
try {
paths = (String[]) method.invoke(storageManager, new Object[0]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return paths;
}
}
本文介绍了如何在Android设备上获取内置和外置SD卡的路径。对于内置SD卡,可以通过Environment.getExternalStorageDirectory().getName()获取。而外置SD卡路径可能有所不同,常见路径包括/mnt/sdcard、/mnt/extsd和/mnt/usbhost1*。
2018

被折叠的 条评论
为什么被折叠?



