usb 和 sdcard 存储路径
原本原厂获取路径逻辑是通过系统属性获取。
sdcard_path = System.getenv(“SECOND_VOLUME_STORAGE”);
usb_path= System.getenv(“THIRD_VOLUME_STORAGE”);
打印出来是null,就用apk获取设备存储路径的方式
1.引用
// An highlighted block
import java.lang.reflect.*;
2.调用
// An highlighted block
sdcard_path = getStoragePath(FirstRun.this,"SD 卡");
usb_path = getStoragePath(FirstRun.this,"U 盘");
3.实现逻辑
// An highlighted block
public static String getStoragePath(Context context,String key) {
String targetpath = "";
StorageManager mStorageManager = (StorageManager) context
.getSystemService(Context.STORAGE_SERVICE);
Class<?> storageVolumeClazz = null;
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
Method getPath = storageVolumeClazz.getMethod("getPath");
Object result = getVolumeList.invoke(mStorageManager);
final int length = Array.getLength(result);
Method getUserLabel = storageVolumeClazz.getMethod("getUserLabel");
for (int i = 0; i < length; i++) {
Object storageVolumeElement = Array.get(result, i);
String userLabel = (String) getUserLabel.invoke(storageVolumeElement);
String path = (String) getPath.invoke(storageVolumeElement);
if(userLabel.contains(key)){
targetpath= path;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return targetpath;
}