StorageManager
生活随笔
收集整理的這篇文章主要介紹了
StorageManager
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取Android設備掛載的所有存儲器android系統提供了Environment.getExternalStorageDirectory()接口獲得存儲器的路徑,但是這個接口往往給的結果并不是我們想要的,在某些設備上它返回的是手機內部存儲,某些設備它返回的手機外部存儲。還有就是某些Android設備支持擴展多個sdcard,這個時候想要獲得所有存儲器的掛載路徑,這個接口是沒有辦法辦到的。怎么獲取Android設備所有存儲器的位置呢?或者說獲得所有的掛載點
系統提供了一個StorageManager,它有一個方法叫getVolumeList,這個方法的返回值是一個StorageVolume數組,StorageVolume類中封裝了掛載路徑,掛載狀態,以及是否可以移除等等信息。但是很可惜,這個方法是隱藏的api,所以我們只能通過反射來調用這個方法了,下面是這個方法的源碼。 public StorageVolume[] getVolumeList() {if (mMountService == null) return new StorageVolume[0];try {Parcelable[] list = mMountService.getVolumeList();if (list == null) return new StorageVolume[0];int length = list.length;StorageVolume[] result = new StorageVolume[length];for (int i = 0; i < length; i++) {result[i] = (StorageVolume)list[i];}return result;} catch (RemoteException e) {Log.e(TAG, "Failed to get volume list", e);return null;}}
? 通過反射,獲取到Android設備所有存儲器。 public class StorageInfo {public String path;public String state;public boolean isRemoveable;public StorageInfo(String path) {this.path = path;}public boolean isMounted() {return "mounted".equals(state);}
} ? public static List<StorageInfo> listAvaliableStorage(Context context) {ArrayList<StorageInfo> storagges = new ArrayList<StorageInfo>();StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);try {Class<?>[] paramClasses = {};Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);getVolumeList.setAccessible(true);Object[] params = {};Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);if (invokes != null) {StorageInfo info = null;for (int i = 0; i < invokes.length; i++) {Object obj = invokes[i];Method getPath = obj.getClass().getMethod("getPath", new Class[0]);String path = (String) getPath.invoke(obj, new Object[0]);info = new StorageInfo(path);File file = new File(info.path);if ((file.exists()) && (file.isDirectory()) && (file.canWrite())) {Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);String state = null;try {Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);state = (String) getVolumeState.invoke(storageManager, info.path);info.state = state;} catch (Exception e) {e.printStackTrace();}if (info.isMounted()) {info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue();storagges.add(info);}}}}} catch (NoSuchMethodException e1) {e1.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}storagges.trimToSize();return storagges;}
? 如何判斷存儲器是內置存儲還是外置存儲呢? StorageVolume這個類中提供了一個isRemovable()接口,通過反射調用它就可以知道存儲器是否可以移除。把可以移除的存儲器認定為外置sdcard,不可移除的存儲器認定為內置存儲器。 Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
? 如何判斷存儲器的掛載狀態呢? 同上面一樣,需要反射系統接口才可以獲取到掛載狀態。下面是代碼片段 Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);state = (String) getVolumeState.invoke(storageManager, info.path);info.state = state;
? 總結 通過反射系統的StorageManager以及StorageVolume類提供的接口,就可以拿到Android設備掛載的所有存儲器路徑,
以及存儲器類型(內置存儲還是外置存儲),還有存儲器的掛載狀態等信息
?
總結
以上是生活随笔為你收集整理的StorageManager的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring5.3.x源码构建
- 下一篇: 毕设无忧|单片机类毕设论文模板