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类中封装了挂载路径,挂载状态,以及是否可以移除等信息。
例如:
代码语言:javascript复制 /**
* 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 mMountPathList
StorageVolume[] 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);
}
有些方法是隐藏的,所以我们要用反射来获取:
代码语言:javascript复制 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;