android之StorageManager介绍
生活随笔
收集整理的這篇文章主要介紹了
android之StorageManager介绍
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
image.png
StorageManager
在Android系統中,常用的存儲介質是Nand Flash;系統的二進制鏡像、
Android的文件系統等通常都保存在Nand Flash 中。
通常使用的Micro-SD卡的管理則是由卷守護進程(Volume Daemon ,vold)去完成的,包括SD卡的插拔事件檢測、掛載、卸載、格式化等。
從Android 2.3開始新增了一個OBB文件系統和StorageManager類用來管理外部存儲上的數據安全。
android.os.storage.StorageManager類的實例化方法需要使用 getSystemService(Contxt.STORAGE_SERVICE)才可以。
我們 可以通過這個服務獲取Android設備上的所有存儲設備。
系統提供了 StorageManager 類,它有一個方法叫getVolumeList(),這個方法的返回值是一個StorageVolume數組,StorageVolume類中封裝了掛載路徑,掛載狀態,以及是否可以移除等信息。
例如:
/*** This method initializes MountPointManager.* * @param context Context to use*/public void init(Context context) {mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);final String defaultPath = getDefaultPath();LogUtils.d(TAG, "init,defaultPath = " + defaultPath); if (!TextUtils.isEmpty(defaultPath)) {mRootPath = ROOT_PATH;}mMountPathList.clear();// check media availability to init mMountPathListStorageVolume[] storageVolumeList = mStorageManager.getVolumeList();if (storageVolumeList != null) {for (StorageVolume volume : storageVolumeList) {MountPoint mountPoint = new MountPoint();mountPoint.mDescription = volume.getDescription(context);mountPoint.mPath = volume.getPath();mountPoint.mIsMounted = isMounted(volume.getPath());mountPoint.mIsExternal = volume.isRemovable();mountPoint.mMaxFileSize = volume.getMaxFileSize();LogUtils.d(TAG, "init,description :" + mountPoint.mDescription + ",path : "+ mountPoint.mPath + ",isMounted : " + mountPoint.mIsMounted+ ",isExternal : " + mountPoint.mIsExternal + ", mMaxFileSize: " + mountPoint.mMaxFileSize);mMountPathList.add(mountPoint);}}IconManager.getInstance().init(context, defaultPath + SEPARATOR); }有些方法是隱藏的,所以我們要用反射來獲取:
public static List<StorageInfo> listAllStorage(Context context) {ArrayList<StorageInfo> storages = new ArrayList<StorageInfo>();StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);try {Class<?>[] paramClasses = {};Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);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);Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);String state = (String) getVolumeState.invoke(storageManager, info.path);info.state = state;Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue();storages.add(info);}}} catch (Exception e) {e.printStackTrace();}storages.trimToSize();return storages;}public static List<StorageInfo> getAvaliableStorage(List<StorageInfo> infos){List<StorageInfo> storages = new ArrayList<StorageInfo>();for(StorageInfo info : infos){File file = new File(info.path);if ((file.exists()) && (file.isDirectory()) && (file.canWrite())) {if (info.isMounted()) {storages.add(info);}}}return storages;總結
以上是生活随笔為你收集整理的android之StorageManager介绍的全部內容,希望文章能夠幫你解決所遇到的問題。