http://blog.csdn.net/roserose0002/article/details/8080495
http://www.cyqdata.com/android/article-detail-36281
http://www.oschina.net/question/157093_108899
http://www.eoeandroid.com/thread-183532-1-1.html (not verified)
1.获取对象:
mStorageManager = (StorageManager)mActivity
.getSystemService(Activity.STORAGE_SERVICE);
2.获取方法:
method = Class.forName("android.os.storage.StorageManager").getMethod("getVolumePaths");
或者...
3.调用方法:
paths = (String[]) method.invoke(mStorageManager);
package com.android.Wecfct.sdcard;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.storage.StorageManager;
public class StorageList {
private Activity mActivity;
private StorageManager mStorageManager;
private Method mMethodGetPaths;
private Method mMethodGetStatus;
String[] paths;
public StorageList(Activity activity) {
mActivity = activity;
if (mActivity != null) {
mStorageManager = (StorageManager)mActivity
.getSystemService(Activity.STORAGE_SERVICE);
try {
mMethodGetPaths = mStorageManager.getClass()
.getMethod("getVolumePaths");
mMethodGetStatus = mStorageManager.getClass()
.getMethod("getVolumeState",String.class);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
public String[] getVolumePaths() {
// String[] paths = null;
try {
paths = (String[]) mMethodGetPaths.invoke(mStorageManager);
System.out.println("reflect patha:"+paths[0]);
System.out.println("reflect pathb:"+paths[1]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return paths;
}
public String getVolumeState(String mountPoint) {
String status = null;
try
{
String tt = "/mnt/external_sdcard";
status = (String) (mMethodGetStatus.invoke(mStorageManager,"/mnt/external_sdcard"));
/* System.out.println(tt);
System.out.println(paths[1]);*/
//status = (String) (mMethodGetStatus.invoke(mStorageManager,paths[1]));
} catch (IllegalArgumentException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("reflect status:"+status);
return status;
}
}
或者
String[] paths = (String[]) sm.getClass().getMethod("getVolumePaths", null).invoke(sm, null);
MOBILE_MEMORY = paths[0];
EXTERNAL_MEMORY = paths[1];
String status = (String) sm.getClass().getMethod("getVolumeState", String.class).invoke(sm, MOBILE_MEMORY);
String status = (String) sm.getClass().getMethod("getVolumeState", String.class).invoke(sm, EXTERNAL_MEMORY);
ps:
transfer "/mnt/external_sdcard" to getVolumeStatus(), not "/mnt/external_sdcard/"