原文地址:http://blog.fidroid.com/post/android/ru-he-zheng-que-huo-de-androidnei-wai-sdqia-lu-jing
/** * 获取外置sd卡路径 * */ public static String getStoragePath(Context mContext) { String extSDPath=null; 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"); Object result = getVolumeList.invoke(mStorageManager); int length = Array.getLength(result); for (int i = 0; i < length; i++) { Object storageVolumeElement = Array.get(result, i); String path = (String) getPath.invoke(storageVolumeElement); boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
//如果removable为false获取到的路径为设备存储(pad内置sd卡路径:/storage/emulated/0)
//如果removable为true获取到的路径为外置sd卡路径:/storage/9C33-6BBD if (removable) { extSDPath=path; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return extSDPath; }
获取到的路径当时我有点疑惑,pad里面显示外置sd卡路径为:/sd卡 而我们获取到的路径为:后经测试,发现此正是外置sd卡路径。/storage/9C33-6BBD