通过正规api得不到外卡路径.
谷歌在源码中给出了得到外卡路径的方法,但标记为隐藏接口,因此api无法访问.
可以通过反射接口得到:
import java.lang.reflect.Method;
import android.os.storage.StorageManager;
public String getPrimaryStoragePath() {
try {
StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
// first element in paths[] is primary storage path
return paths[0];
} catch (Exception e) {
Log.e(TAG, "getPrimaryStoragePath() failed", e);
}
return null;
}
public String getSecondaryStoragePath() {
try {
StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE);
Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null);
String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null);
// second element in paths[] is secondary storage path
return paths[1];
} catch (Exception e) {
Log.e(TAG, "getSecondaryStoragePath() failed", e);
}
return null;
}
public String getStorageState(String path) {
try {
StorageManager sm = (StorageManager) getSystemService(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;
}
如果楼主有源码,可以去找StorageManager这个类,拉到文件最下方,就可以看到那三个隐藏接口.