public static final String ROOT_PATH = "/storage/emulated";
public final static String PHONE_STORAGE_PATH = "/storage/emulated/0";
public final static String SDCARD_STORAGE_PATH = "/storage/sdcard0";
public final static String USB_STORAGE_PATH = "/storage/usbdisk";
public final static String STORAGE_ROOT_PATH = "/storage";
private final static String METHOD_GET_VOLUME_LIST = "getVolumeList";
private final static String METHOD_GET_VOLUME_STATE = "getState";private final static String METHOD_IS_REMOVABLE = "isRemovable";
private final static String METHOD_GET_PATH = "getPathFile";
private final static String METHOD_GET_UUID = "getUuid";
private Context mContext;
private StorageManager mStorageManager;
// 初始化manager对象
mStorageManager = (StorageManager) mContext.getSystemService(Context.STORAGE_SERVICE);// 反射调用实例
private void executeInit() {
if (mStorageManager != null) {
Method mGetVolumeListMethod = null;
Object[] mStorageVolumeList = null;
try {
// 获取StorageManager类的 getVolumeList()隐藏方法
mGetVolumeListMethod = mStorageManager.getClass().getMethod(METHOD_GET_VOLUME_LIST, new Class[0]);
// 激发调用StorageManager类的 getVolumeList()隐藏方法,并返回 数据列表对象 StorageVolume[],将数据保存给Object[]
mStorageVolumeList = (Object[]) mGetVolumeListMethod.invoke(mStorageManager, new Object[0]);if (mStorageVolumeList != null && mStorageVolumeList.length > 0) {
int mStorageVolumeCount = mStorageVolumeList.length;
for (int i = 0; i < mStorageVolumeCount; i++) {
// 通过反射方式,使用数据元素对象获取到类反射的方法getPathFile(),并通过该元素对象激发该方法,返回得到File对象
File storagePath = (File) mStorageVolumeList[i].getClass().getMethod(METHOD_GET_PATH, new Class[0]).invoke(mStorageVolumeList[i], new Object[0]);
// 类似以上
Boolean isRemovable = (Boolean) mStorageVolumeList[i].getClass().getMethod(METHOD_IS_REMOVABLE, new Class[0])
.invoke(mStorageVolumeList[i], new Object[0]);
// 类似以上
String mUuid = (String) mStorageVolumeList[i].getClass().getMethod(METHOD_GET_UUID, new Class[0]).invoke(mStorageVolumeList[i], new Object[0]);
String filepath = storagePath.getAbsolutePath();
if (!TextUtils.isEmpty(storagePath.getAbsolutePath())) {
String state = (String) mStorageVolumeList[i].getClass()
.getMethod(METHOD_GET_VOLUME_STATE, new Class[0])
.invoke(mStorageVolumeList[i], new Object[0]);
if (state != null && MOUNTED.equals(state)) {
if (!isRemovable) {
LogUtils.i(TAG, "mPhoneStorage is mounted storagePath:" + storagePath);
mPhoneStorage.init(storagePath);
mPhoneStorage.setMountState(true);
} else if (mUuid != null && filepath.equals(STORAGE_ROOT_PATH + "/" + mUuid)
|| SDCARD_STORAGE_PATH.equals(filepath)) {
LogUtils.i(TAG, "mSDCardStorage is mounted storagePath:" + storagePath);
mSDCardStorage.init(storagePath);
mSDCardStorage.setMountState(true);
} else if (USB_STORAGE_PATH.equals(filepath)) {
//mUSBStorageInfo = new StorageUtilImpl.StorageInfo(storagePath);
//mUSBStorageInfo.setMountState(true);
}
}
}
}
}
} catch (NoSuchMethodException ex) {
LogUtils.e("executeInit NoSuchMethodException " + ex.toString());
} catch (IllegalArgumentException ex) {
LogUtils.e("executeInit IllegalArgumentException " + ex.toString());
} catch (IllegalAccessException ex) {
LogUtils.e("executeInit IllegalAccessException " + ex.toString());
} catch (InvocationTargetException ex) {
LogUtils.e("executeInit InvocationTargetException " + ex.toString());
}
}
}
//
public static Class EnvironmentEx; private static Method getExternalStorageMethod;// 通过反射调用类 EnvironmentEx的静态方法
public static File getExternalStorage(){ try { if(getExternalStorageMethod==null){ getExternalStorageMethod = EnvironmentEx.getMethod("getExternalStoragePath"); } return ((File)getExternalStorageMethod.invoke(null)); } catch (Exception e) { e.printStackTrace(); } return new File("/storage/sdcard0"); }