本方式可以获得内部存储设备地址、SD卡地址、USB设备地址,兼容性能达到99%(别问我为什么这么保证,因为是借鉴了Android设置->存储页面的源码)。
由于调用了几个被@hide的方法,所以采用了反射。
具体代码如下:
public static ListgetAllExternalStorage(Context context) { List storagePath = new ArrayList<>(); StorageManager storageManager = (StorageManager) context.getSystemService(STORAGE_SERVICE); StorageVolume[] storageVolumes; try { Method getVolumeList = StorageManager.class.getDeclaredMethod("getVolumeList"); storageVolumes = (StorageVolume[]) getVolumeList.invoke(storageManager); Method getVolumeState = StorageManager.class.getDeclaredMethod("getVolumeState", String.class); for (StorageVolume storageVolume : storageVolumes) { String desc = storageVolume.getDescription(context); Log.i(TAG, "storageVolume name--->" + desc); Method getPath = StorageVolume.class.getMethod("getPath"); String path = (String) getPath.invoke(storageVolume); Log.i(TAG, "StoragePath--->" + path); //这里需要用StorageManager反射调用getVolumeState函数,而不应该用StorageVolume的getState方法,因为可能会报错 String state = (String) getVolumeState.invoke(storageManager, path); Log.i(TAG, "storageVolume State--->" + state); if (Environment.MEDIA_MOUNTED.equals(state)) { HomeDirBean bean = new HomeDirBean(path, desc); storagePath.add(bean); } } } catch (Exception e) { Log.e(TAG, e.getMessage()); } return storagePath; }
这里需要注意,可能有小伙伴会问,既然StorageVolume类有getState方法,为啥还要用StorageManager反射调用getVolumeState方法,并传入path地址,而在源码里,StorageManager的getVolumeState的方法的实现,也是将path重新创建为StorageVolume类,然后再调用其getState方法,我们这样做成这不是多此一举吗?
源码截图如下:
答案当然不是了,不然我也不会放弃性能去反射那个方法去装这个逼了。主要原因是@hide的这个方法里,mountPoint被重新打包成StorageVolume时,这相当于系统去创建的一个StorageVolume实例,自然可以执行它的所有方法。而如果是应用直接调用,在被打包时,很多方法被隐藏了,比如这个getState方法,这时候应用就会报错,找不到该方法。
先简单写到这,以后有补充再添加。
服装店经营模拟游戏 汉化版v0.1
服装店经营模拟游戏是一款玩法简单有趣的3D类型模拟经营游戏,
从细胞到奇点 最新版本v32.42
从细胞到奇点官方正版是一款科学放置游戏,在游戏中玩家需要点击
MY LITTLE PONY魔法公主 安卓版v10.0.0
MY LITTLE PONY魔法公主是一款卡通风格的模拟养成
星露谷物语sve 安卓版v1.5.6.52
星露谷物语sve手机版是游戏的超大拓展Mod内容,全称Sta
动物园大亨 手机版v2.0
动物园大亨是一款模拟动物园经营的休闲放置游戏,采用了卡通画风