一开始没找到合适的方法,后来看到系统通知栏中直接显示了对应类型,
找到对应代码但在普通app中用不了,那只能上反射大法了
public class SDCardUtil{ private static String TAG = "SDCardUtil"; public static HashMap getStorageType(Context pContext) { HashMap<String,String> map = new HashMap<>(); map.clear(); StringBuffer buffer = new StringBuffer(); final StorageManager storageManager = (StorageManager) pContext.getSystemService(Context.STORAGE_SERVICE); try { //获取类型是U盘还是SD卡 final Method getVolumes = storageManager.getClass().getMethod("getVolumes"); final List<Object> invokeVolumes = (List<Object>) getVolumes.invoke(storageManager); final Class<?> volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo"); final Method getPath = volumeInfoClazz.getMethod("getPath"); final Method getDisk = volumeInfoClazz.getMethod("getDisk"); final Class<?> diskInfoClazz = Class.forName("android.os.storage.DiskInfo"); final Method getDescription = diskInfoClazz.getMethod("getDescription"); for (int i = 0; i < invokeVolumes.size(); i++) { final Object volumeInfo = invokeVolumes.get(i); File invokePath = (File) getPath.invoke(volumeInfo); Log.e(TAG, "invokePath:"+invokePath.getPath()); if ("/data".equals(invokePath.getPath()) || "/storage/emulated".equals(invokePath.getPath())) continue; Object invokeDiskInfo = getDisk.invoke(volumeInfo); String description = (String)getDescription.invoke(invokeDiskInfo); Log.e(TAG, "getDescription:"+description); map.put(invokePath.getPath(), description); } return map; } }