以前写了一篇文章也是说获取内外SD卡路径的:Android获取手机自带SDCard和外置SDCard路径,但是只适用于5.0及以下的系统。
在Android Studio中使用,StorageVolume是在Android7.0(api24)里面才添加的类
/**
* 获取SD卡路径
* @param mContext
* @param is_removable SD卡是否可移除,不可移除的是内置SD卡,可移除的是外置SD卡
* @return
*/
public static String getStoragePath(Context mContext, boolean is_removable) {
StorageManager mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
Method getState = storageVolumeClazz.getMethod("getState");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String path = (String) getPath.invoke(storageVolumeElement);
String state = (String) getState.invoke(storageVolumeElement);
boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
if (is_removable == removable) {
return path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
参考文章:http://blog.csdn.net/u010838555/article/details/51783384