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. jdbc连接远程数据库进行操作

    链接远程数据库的时候,要把获得链接的url进行修改 1 package com.test; import java.sql.Connection; import java.sql.DriverMana ...

  2. OFBIZ 10.04 开发环境搭建(ofbiz+mysql+eclipse)

    1.下载安装 JDK1.6,并设置环境变量 在“我的电脑”上点右键—>“属性”—>“高级”—> “环境变量(N)”.    新建系统变量JAVA_HOME:C:Program Fil ...

  3. WordPress主题设置插件,让你的站点有电脑、手机双主题

    我们建站的时候总是会优先考虑自适应的主题,但是与之对应,免费的自适应主题都调用国外公共资源,访问速度不太理想.加上wordpress未经优化之前,本身也没有极高的访问效率.所以大家可以下载这款“主题设 ...

  4. 【转】Mac系统新建txt文本文件技巧

    很多时候,我们需要在 Mac 中创建 txt 文件来记录一些信息,但是打开系统自带的文本编辑默认并不是创建 txt 文本文件 方法一: 打开终端,cd 到想要创建 txt 文本文件的目录(如桌面) 1 ...

  5. hdu5798

    官方题解: 考虑去掉abs符号,发现只有相邻两个数的最高位被影响了才会影响abs的符号,所以可以按照最高位不一样的位置分类,之后考虑朴素枚举x从0到2^20,每次的复杂度是O(400),无法通过,考虑 ...

  6. CentOS7.5下gnome-terminal+vim的solarized配色方案

    1.简介 Solarized是一款包括浅色和深色的配色方案,适用于很多应用,可以让你的应用看起来更加漂亮!官网地址:http://ethanschoonover.com/solarized 2.设置g ...

  7. 深刻剖析VuGen脚本录制原理

    扩展:深刻剖析VuGen脚本录制原理 LR中的VuGen组件,主要扮演一个proxy server的角色,在录制脚本时,记录下用户和服务器交互,然后自动生成脚本语言.在接下来的重放,或者大批量地加压时 ...

  8. 将cmake文件转化为vs方便代码阅读与分析

    下面通过“chengxuyuancc”同学的图来说明.通过cmake将winafl cmake编译方式转化为vs2015,方便源码阅读与分析. 1.到官网下载cmake软件.启动图形版 2.选择源码目 ...

  9. Flask实战第46天:完成前台登录功能

    后台逻辑 首先进行表单验证, 编辑front.froms.py ... class SignInForm(BaseForm): telephone = StringField(validators=[ ...

  10. 【剑指offer】顺时针打印数组

    顺时针打印数组 题意 例如我们有一个二维数组,如下 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 现在要按照顺时针打印出来,结果应该为: 1 2 3 4 8 12 16 ...