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()]);
}

[Android Pro] StorageManager简介的更多相关文章

  1. Android资源文件简介

    Android资源文件简介 1. Android应用资源的作用 (1) Android项目中文件分类 在Android工程中, 文件主要分为下面几类 : 界面布局文件, Java src源文件, 资源 ...

  2. Android Action Bar简介

    Android Action Bar简介 Design: Action Bar Action Bar是在屏幕顶端的一部分内容,通常在整个app进行中都保持存在. 它提供了几个关键的功能: 1.使得重要 ...

  3. eclipse安装androidSDK地址,Android SDK Manager简介

    eclipse安装android插件地址:https://dl-ssl.google.com/android/eclipse 这个和安装其他插件方式一样:Help—Install New Softwa ...

  4. 【Android 系统开发】 Android 系统启动流程简介

    作者 : 万境绝尘 (octopus_truth@163.com) 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/3889548 ...

  5. android.net.Uri 简介 API

    android.net.Uri 简介 public abstract class android.net.Uri extends Object implements Parcelable, Compa ...

  6. IDA Pro基本简介

    IDA Pro基本简介 IDA加载完程序后,3个立即可见的窗口分别为IDA-View,Named,和消息输出窗口(output Window). IDA图形视图会有执行流,Yes箭头默认为绿色,No箭 ...

  7. Android Notification通知简介

    Android Notification通知简介 根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面 ...

  8. Android MediaPlayer 基础简介

    本文链接: Android MediaPlayer 基础简介 简单介绍MediaPlayer的基本概念,状态,常用的方法与监听器. 什么是MediaPlayer MediaPlayer类可以用来播放音 ...

  9. [Android Pro] Android下toolbox简介

    toolbox是Android 自带的提供shell命令的软件.有点类似于busybox,但功能上好像弱很多.其源码可以从Android source code 中system/core/toolbo ...

随机推荐

  1. 全国省市区数据SQL - 省市区

    转载:https://www.cnblogs.com/flywind/p/6036801.html

  2. 关于ueditor在Java中文件上传问题,404问题

    问题困扰了两天,部署要求导入到webcontent下,我导入到了整个项目目录下,自己粗心犯错,导致页面访问不到404. 解决了上面的问题,试着进行文件上传,却一直找不到图片: 调出浏览器控制台: 刚开 ...

  3. 在写一次epoll

    epoll & select & poll只能处理IO相关的操作,epoll每一个操作必须注册到时间监控机制中,并且还需要进程或者线程进行管理. 多进程/多线程 和epoll相比较 e ...

  4. 三:基于Storm的实时处理大数据的平台架构设计

    一:元数据管理器==>元数据管理器是系统平台的“大脑”,在任务调度中有着重要的作用[1]什么是元数据?--->中介数据,用于描述数据属性的数据.--->具体类型:描述数据结构,数据的 ...

  5. poj 1088(记忆化搜索)

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 88560   Accepted: 33212 Description ...

  6. 微信小程序-ios系统-下拉上拉出现白色,如何处理呢?

    这几天做小程序,有些页面都是全屏的背景,在安卓上背景是固定的,而在ios上上拉下拉出现白色,测试说体验不太好,一开始我以为是下拉上拉刷新造成的,关闭了依然是这样.为了体验好点,可以按一下解决: 方式一 ...

  7. maven项目的pom.xml文件详解

    <project xmlns="http://maven.apache.org/POM/4.0.0 " 2 xmlns:xsi="http://www.w3.org ...

  8. Linux下几个命令的技巧

    Ctrl的组合键+a,移动到一行命令的首部+e,移动到一行命令的尾部+左右键,以单词为单位左右移动+u,删除光标之前的所有内容+k,删除光标之后的所有内容Alt+.为引用上一个命令的最后一个参数 还有 ...

  9. 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) 日常训练

    A - Archery Tournament 题目大意:按时间顺序出现靶子和射击一个位置,靶子的圆心为(x, y)半径为r,即圆与x轴相切,靶子不会重叠,靶子被击中后消失, 每次射击找出哪个靶子被射中 ...

  10. MySQL常用的数学函数

    在使用mysql自带的函数要慎重,说是会影响数据执行效率,代价太大.这个也要区分开,区分快软件的引用范畴,比如说内部系统业务逻辑比较复杂,功能点很细,但是并发量不是很大,这个时候用MySQL自带的函数 ...