/**
* 获取内置SD卡路径
*
*/
public static String getPrimaryStoragePath() {
try {
StorageManager sm = (StorageManager) mContext.getSystemService(Context
.STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
return paths[0];
} catch (Exception e) {
Log.e(TAG, "getPrimaryStoragePath() failed", e);
}
return null;
}
/**
* 获取外置SD卡路径
*
*/
public static String getSecondaryStoragePath() {
try {
StorageManager sm = (StorageManager) mContext.getSystemService(Context
.STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
return paths[1];
} catch (Exception e) {
Log.e(TAG, "getSecondaryStoragePath() failed", e);
}
return null;
}
public static String getStorageState(String path) {
try {
StorageManager sm = (StorageManager) mContext.getSystemService(Context
.STORAGE_SERVICE);
Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[] {String.class});
String state = (String) getVolumeStateMethod.invoke(sm, path);
return state;
} catch (Exception e) {
Log.e(TAG, "getStorageState() failed", e);
}
return null;
}
/**
* 判断外置sd卡是否存在
*
*/
private static boolean isExternalStorageMounted() {
StorageManager sm = (StorageManager) mContext.getSystemService(Context
.STORAGE_SERVICE);
final StorageVolume[] volumes = sm.getVolumeList();
for (StorageVolume v : volumes) {
if (v.isRemovable()) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(v.getPathFile())))
return true;
}
}
return false;
}