StorageManager

StorageManager is the interface to the systems storage service. The storage manager handles storage-related items such as Opaque Binary Blobs (OBBs).

OBBs contain a filesystem that maybe be encrypted on disk and mounted on-demand from an application. OBBs are a good way of providing large amounts of binary assets without packaging them into APKs as they may be multiple gigabytes in size. However, due to their size, they're most likely stored in a shared storage pool accessible from all programs. The system does not guarantee the security of the OBB file itself: if any program modifies the OBB, there is no guarantee that a read from that OBB will produce the expected output.

Get an instance of this class by calling getSystemService(java.lang.String) with an argument of STORAGE_SERVICE.

从Android 2.3开始新增了一个OBB文件系统和StorageManager类用来管理外部存储上的数据安全。

如果我们设计一款资源包含比较多的游戏,可能你会发现最终生成的APK文件可能高达300MB,但是APK文件很大导致Android系统无法正常安装,而这么大其实都是游戏中用到的资源文件,我们放到SD卡上可能其他应用也可以访问,比如说系统的图片管理器会索引游戏中的图片资源,而音乐播放器也会索引资源中的音乐,所以Android 2.3的OBB文件(Opaque Binary Blob)可以很好的解决大文件在SD卡上分离出APK文件,同时别的程序没有权限访问这样一种隔离的文件系统。
  android.os.storage.StorageManager类的实例化方法需要使用 getSystemService(Contxt.STORAGE_SERVICE)才可以,eoe再次提醒这是一个API Level至少为9才能调用的类,注意SDK版本以及目标设备的固件。

我们知道android上一般都有外置的存储卡, 
但是通过Environment.getExternalStorageDirectory()获取的是内置的存储卡位置 (也有的手机可以在系统中修改默认存储) 那么如何获取外置存储卡的位置呢?

通过StorageManager来获取多个sdcard,比使用cat /proc/mounts要好:

public static String[] getStoragePaths(Context cxt) {
List<String> pathsList = new ArrayList<String>();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.GINGERBREAD) {
StringBuilder sb = new StringBuilder();
try {
pathsList.addAll(new SdCardFetcher().getStoragePaths(new FileReader("/proc/mounts"), sb));
} catch (FileNotFoundException e) {
e.printStackTrace();
File externalFolder = Environment.getExternalStorageDirectory();
if (externalFolder != null) {
pathsList.add(externalFolder.getAbsolutePath());
}
}
} else {
StorageManager storageManager = (StorageManager) cxt.getSystemService(Context.STORAGE_SERVICE);
try {
Method method = StorageManager.class.getDeclaredMethod("getVolumePaths");
method.setAccessible(true);
Object result = method.invoke(storageManager);
if (result != null && result instanceof String[]) {
String[] pathes = (String[]) result;
StatFs statFs;
for (String path : pathes) {
if (!TextUtils.isEmpty(path) && new File(path).exists()) {
statFs = new StatFs(path);
if (statFs.getBlockCount() * statFs.getBlockSize() != 0) {
pathsList.add(path);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
File externalFolder = Environment.getExternalStorageDirectory();
if (externalFolder != null) {
pathsList.add(externalFolder.getAbsolutePath());
}
}
}
return pathsList.toArray(new String[pathsList.size()]);
}

最新文章

  1. 这可能是史上最全的CSS自适应布局总结教程
  2. Python之路【第二十篇】Tornado框架
  3. Linux 使用本地yum源及软件包管理
  4. c#网络通信框架networkcomms内核解析之九 自定义处理方法的运行机制
  5. 发布HTML5 RTS游戏-古代战争
  6. 软件工程随堂小作业——(C++)
  7. IUSR和IIS_IUSRS
  8. [转] 字符串模式匹配算法——BM、Horspool、Sunday、KMP、KR、AC算法一网打尽
  9. Windows下命令行连接mysql及导入sql文件
  10. MVC 使用AJAX POST上传图片的方式
  11. memcached session共享
  12. PHP.INI常用设置一览表(持续更新)
  13. windbg Symbol file path
  14. WCF、Web API、WCF REST、Web Service的区别
  15. 解决xshell连接不上Ubuntu
  16. Tsung脚本中使用动态参数(一)---直接在脚本里编写Erlang代码
  17. 在CYGWIN下编译和运行软件Bundler ,以及PMVS,CMVS的编译与使用
  18. Linux学习笔记10
  19. VMware workstation12 密匙
  20. 第三篇 功能实现(1) (Android学习笔记)

热门文章

  1. 关于:TypeConverter 无法从 System.String 转换
  2. MySQL:按后缀缀批量删除表格
  3. java中的三元运算符
  4. POJ-3268
  5. LeetCode解题报告—— Median of Two Sorted Arrays
  6. Delphi获取毫秒级时间戳
  7. es6 map数据类型,要比set还很多
  8. Java实现POI读取Excel文件,兼容后缀名xls和xlsx
  9. HDU 5128.The E-pang Palace-计算几何
  10. Centos安装Sun的JDK及测试