利用反射得到android存储路径

获得android手机的存储路径:

public String getPrimaryStoragePath(){
try{
StorageManager sm = (StorageManager) context.getSystemService(context.STORAGE_SERVICE);
Method m = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) m.invoke(sm, null);
return paths[0];
}catch(Exception e){
Log.e("xxxxxxxxxx", "getPrimaryStoragePath() failed", e);
}
return null;
} public String getSecondaryStoragePath(){
try{
StorageManager sm = (StorageManager) context.getSystemService(context.STORAGE_SERVICE);
Method m = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) m.invoke(sm, null);
return paths[1];
}catch(Exception e){
Log.e("xxxxxxxxxx", "getSecondaryStoragePath() failed", e);
}
return null;
}

在StorageManager类中有三个隐藏的方法,用反射得到最后那个方法就可以获得android手机所有可存储路径

/**
* Gets the state of a volume via its mountpoint.
* @hide
*/
public String getVolumeState(String mountPoint) {
try {
return mMountService.getVolumeState(mountPoint);
} catch (RemoteException e) {
Log.e(TAG, "Failed to get volume state", e);
return null;
}
}
/**
* Returns list of all mountable volumes.
* @hide
*/
public StorageVolume[] getVolumeList() {
try {
Parcelable[] list = mMountService.getVolumeList();
if (list == null) return new StorageVolume[0];
int length = list.length;
StorageVolume[] result = new StorageVolume[length];
for (int i = 0; i < length; i++) {
result[i] = (StorageVolume)list[i];
}
return result;
} catch (RemoteException e) {
Log.e(TAG, "Failed to get volume list", e);
return null;
}
}
/**
* Returns list of paths for all mountable volumes.
* @hide
*/
public String[] getVolumePaths() {
StorageVolume[] volumes = getVolumeList();
if (volumes == null) return null;
int count = volumes.length;
String[] paths = new String[count];
for (int i = 0; i < count; i++) {
paths[i] = volumes[i].getPath();
}
return paths;
}
上一篇:oracle中动态SQL详解


下一篇:JSF request参数传递