android文件存储位置切换

  最近有个需求,助手的google卫星地图和OpenCycleMap下载的离线地图数据,要能够在内置存储和外置存储空间之间切换,因为离线瓦片数据非常大,很多户外用户希望将这些文件存储在外置TF卡上,不占用内置存储空间,所以把最近研究的整理了下,分享给大家。

  需要考虑和遇到的问题(主要是不同手机、不同系统的兼容性):

  1.这样获取手机所有挂载的存储器?

     Android是没有提供显式的接口的,首先肯定是要阅读系统设置应用“存储”部分的源码,看存储那里是通过什么方式获取的。最后找到StorageManager和StorageVolume这2个重要的类,然后通过反射获取StorageVolume[]列表。

  2.用什么标示一个存储器的唯一性?

   存储路径?不行(有些手机不插TF卡,内置存储路径是/storage/sdcard0,插上TF卡后,内置存储路径变成/storage/sdcard1,TF卡变成/storage/sdcard0)。

   存储卡名称?不行(可能会切换系统语言,导致名称匹配失败,名称的resId也不行,较低的系统版本StorageVolume没有mDescriptionId这一属性)。

     经过测试,发现使用mStorageId可以标示存储器的唯一性,存储器数量改变,每个存储器的id不会改变。

  3.如何获得存储器的名称?

    经测试,不同的手机主要有3种获取存储器名换的方法:getDescription()、getDescription(Context context)、先获得getDescriptionId()再通过resId获取名称。

  4.任务文件下载一半时,切换文件保存存储器,怎么处理?

    有2种方案:

    4.1 切换时,如果新的存储空间足够所有文件转移,先停止所有下载任务,将所有下载完和下载中的文件拷贝到新的存储空间,然后再更新下载数据库下载任务的存储路径,再恢复下载任务;

    4.2 切换时,先拷贝所有下载完成的文件到新的存储空间,下载任务继续下载,下载完成再移动到新的存储空间。

  5.在4.4系统上,第三方应用无法读取外置存储卡的问题。(参考“External Storage”)

   google为了在程序卸载时,能够完全彻底的将程序所有数据清理干净,应用将不能向2级存储区域写入文件。

   “The WRITE_EXTERNAL_STORAGE permission must only grant write access to the primary external storage on a device. Apps must not be allowed to write to secondary external storage devices, except in their package-specific directories as allowed by synthesized permissions. Restricting writes in this way ensures the system can clean up files when applications are uninstalled.”

   要能够在4.4系统上TF卡写入文件,必须先root,具体方法可以google。

   所以4.4系统上,切换会导致文件转移和下载失败,用户如果要切换到TF卡,至少需要提醒用户,并最好给出4.4上root解决方法。

 

  以下是获取存储器的部分代码:

  

  1 public static class MyStorageVolume{
  2         public int mStorageId;
  3         public String mPath;
  4         public String mDescription;
  5         public boolean mPrimary;
  6         public boolean mRemovable;
  7         public boolean mEmulated;
  8         public int mMtpReserveSpace;
  9         public boolean mAllowMassStorage;
 10         public long mMaxFileSize;  //最大文件大小。(0表示无限制)
 11         public String mState;      //返回null
 12 
 13         public MyStorageVolume(Context context, Object reflectItem){
 14             try {
 15                 Method fmStorageId = reflectItem.getClass().getDeclaredMethod("getStorageId");
 16                 fmStorageId.setAccessible(true);
 17                 mStorageId = (Integer) fmStorageId.invoke(reflectItem);
 18             } catch (Exception e) {
 19             }
 20 
 21             try {
 22                 Method fmPath = reflectItem.getClass().getDeclaredMethod("getPath");
 23                 fmPath.setAccessible(true);
 24                 mPath = (String) fmPath.invoke(reflectItem);
 25             } catch (Exception e) {
 26             }
 27 
 28             try {
 29                 Method fmDescriptionId = reflectItem.getClass().getDeclaredMethod("getDescription");
 30                 fmDescriptionId.setAccessible(true);
 31                 mDescription = (String) fmDescriptionId.invoke(reflectItem);
 32             } catch (Exception e) {
 33             }
 34             if(mDescription == null || TextUtils.isEmpty(mDescription)){
 35                 try {
 36                     Method fmDescriptionId = reflectItem.getClass().getDeclaredMethod("getDescription");
 37                     fmDescriptionId.setAccessible(true);
 38                     mDescription = (String) fmDescriptionId.invoke(reflectItem, context);
 39                 } catch (Exception e) {
 40                 }
 41             }
 42             if(mDescription == null || TextUtils.isEmpty(mDescription)){
 43                 try {
 44                     Method fmDescriptionId = reflectItem.getClass().getDeclaredMethod("getDescriptionId");
 45                     fmDescriptionId.setAccessible(true);
 46                     int mDescriptionId = (Integer) fmDescriptionId.invoke(reflectItem);
 47                     if(mDescriptionId != 0){
 48                         mDescription = context.getResources().getString(mDescriptionId);
 49                     }
 50                 } catch (Exception e) {
 51                 }
 52             }
 53 
 54             try {
 55                 Method fmPrimary = reflectItem.getClass().getDeclaredMethod("isPrimary");
 56                 fmPrimary.setAccessible(true);
 57                 mPrimary = (Boolean) fmPrimary.invoke(reflectItem);
 58             } catch (Exception e) {
 59             }
 60 
 61             try {
 62                 Method fisRemovable = reflectItem.getClass().getDeclaredMethod("isRemovable");
 63                 fisRemovable.setAccessible(true);
 64                 mRemovable = (Boolean) fisRemovable.invoke(reflectItem);
 65             } catch (Exception e) {
 66             }
 67 
 68             try {
 69                 Method fisEmulated = reflectItem.getClass().getDeclaredMethod("isEmulated");
 70                 fisEmulated.setAccessible(true);
 71                 mEmulated = (Boolean) fisEmulated.invoke(reflectItem);
 72             } catch (Exception e) {
 73             }
 74 
 75             try {
 76                 Method fmMtpReserveSpace = reflectItem.getClass().getDeclaredMethod("getMtpReserveSpace");
 77                 fmMtpReserveSpace.setAccessible(true);
 78                 mMtpReserveSpace = (Integer) fmMtpReserveSpace.invoke(reflectItem);
 79             } catch (Exception e) {
 80             }
 81 
 82             try {
 83                 Method fAllowMassStorage = reflectItem.getClass().getDeclaredMethod("allowMassStorage");
 84                 fAllowMassStorage.setAccessible(true);
 85                 mAllowMassStorage = (Boolean) fAllowMassStorage.invoke(reflectItem);
 86             } catch (Exception e) {
 87             }
 88 
 89             try {
 90                 Method fMaxFileSize = reflectItem.getClass().getDeclaredMethod("getMaxFileSize");
 91                 fMaxFileSize.setAccessible(true);
 92                 mMaxFileSize = (Long) fMaxFileSize.invoke(reflectItem);
 93             } catch (Exception e) {
 94             }
 95 
 96             try {
 97                 Method fState = reflectItem.getClass().getDeclaredMethod("getState");
 98                 fState.setAccessible(true);
 99                 mState = (String) fState.invoke(reflectItem);
100             } catch (Exception e) {
101             }
102         }
103 
104         /**
105          * 获取Volume挂载状态, 例如Environment.MEDIA_MOUNTED
106          */
107         public String getVolumeState(Context context){
108             return StorageVolumeUtil.getVolumeState(context, mPath);
109         }
110 
111         public boolean isMounted(Context context){
112             return getVolumeState(context).equals(Environment.MEDIA_MOUNTED);
113         }
114 
115         public String getDescription(){
116             return mDescription;
117         }
118 
119         /**
120          * 获取存储设备的唯一标识
121          */
122         public String getUniqueFlag(){
123             return "" + mStorageId;
124         }
125 
126         /*public boolean isUsbStorage(){
127             return mDescriptionId == android.R.string.storage_usb;
128         }*/
129 
130         /**
131          * 获取目录可用空间大小
132          */
133         public long getAvailableSize(){
134             return StorageVolumeUtil.getAvailableSize(mPath);
135         }
136 
137         /**
138          * 获取目录总存储空间
139          */
140         public long getTotalSize(){
141             return StorageVolumeUtil.getTotalSize(mPath);
142         }
143 
144         @Override
145         public String toString() {
146             return "MyStorageVolume{" +
147                     "\nmStorageId=" + mStorageId +
148                     "\n, mPath='" + mPath + '\'' +
149                     "\n, mDescription=" + mDescription +
150                     "\n, mPrimary=" + mPrimary +
151                     "\n, mRemovable=" + mRemovable +
152                     "\n, mEmulated=" + mEmulated +
153                     "\n, mMtpReserveSpace=" + mMtpReserveSpace +
154                     "\n, mAllowMassStorage=" + mAllowMassStorage +
155                     "\n, mMaxFileSize=" + mMaxFileSize +
156                     "\n, mState='" + mState + '\'' +
157                     '}' + "\n";
158         }
159     }
存储信息MyStorageVolume

 

 1 public static List<MyStorageVolume> getVolumeList(Context context){
 2         List<MyStorageVolume> svList = new ArrayList<MyStorageVolume>(3);
 3         StorageManager mStorageManager = (StorageManager)context
 4                 .getSystemService(Activity.STORAGE_SERVICE);
 5         try {
 6             Method mMethodGetPaths = mStorageManager.getClass().getMethod("getVolumeList");
 7             Object[] list = (Object[]) mMethodGetPaths.invoke(mStorageManager);
 8             for(Object item : list){
 9                 svList.add(new MyStorageVolume(context, item));
10             }
11         } catch (Exception e) {
12             e.printStackTrace();
13         }
14         return svList;
15     }
获取所有存储器

 

github上的测试例子:

https://github.com/John-Chen/BlogSamples/tree/master/StorageTest

  

如果还有什么地方没有考虑到的,欢迎讨论。  

 

转载于:https://www.cnblogs.com/John-Chen/p/4216754.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/472540.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[c]扫雷

题目描述 扫雷游戏是一款十分经典的单机小游戏。在n行m列的雷区中有一些格子含有地雷&#xff08;称之为地雷格&#xff09;&#xff0c;其他格子不含地雷&#xff08;称之为非地雷格&#xff09;。 玩家翻开一个非地雷格时&#xff0c;该格将会出现一个数字——提示周围格子中…

树莓派python gpio 模仿iic_Adafruit的树莓派教程:GPIO配置

概览树莓派最令人兴奋的特点之一是它有一个GPIO连接器可以用来接其他的硬件设备。GPIO连接器实际上是由许多不同类型的接口组成的&#xff1a;真正的GPIO(General Purpose Input Output,通用输入/输出)针脚&#xff0c;你可以用来控制LED灯的开和关。I2C(Inter&#xff0d;Inte…

netcore docker_让.NetCore程序跑在任何有docker的地方

一.分别在Windows/Mac/Centos上安装DockerWindows上下载地址&#xff1a;https://docs.docker.com/docker-for-windows/install/&#xff08;window上安装的常见问题和解决方案请参考下方步骤六&#xff09;Mac上下载地址&#xff1a;https://hub.docker.com/editions/communit…

MapReduce 编程实践

文章目录1. MapReduce 作业流程2. 实践2.1 启动 hadoop2.2 创建 java 项目2.3 MapReduce shell2.4 MapReduce Web UI3. MapReduce 编程实践&#xff1a;统计对象中的某些属性参考书&#xff1a;《Hadoop大数据原理与应用》1. MapReduce 作业流程 2. 实践 2.1 启动 hadoop sta…

linux c代码出现段错误,Linux下段错误(C语言)

问题描述&#xff1a;在Linux下编程有时会出现段错误的提醒&#xff0c;出现这种错误有可能是因为以下几种原因1.数组越界&#xff1a;如果在初始化或者接收输入时内容超过了定义好的数组元素个数时会出现段错误&#xff0c;Linux的数组越界检查做的不是很好&#xff0c;在编译…

micropython webrepl_4-5 MicroPython WebREPL 命令行交互环境设置-2 接入点模式

在这一节教程里我们将一起学习如何为NodeMCU在接入点模式下设置MicroPython网络命令行交互环境(以下简称: WebREPL)。所谓接入点模式就是NodeMCU可以建立WIFI网络供其他设备接入。如下图所示。ESP8266-NodeMCU接入点(Access Point)工作模式在开始设置WebREPL以前请确认您已经完…

基于XMPP实现的Openfire的配置安装+Android客户端的实现

http://blog.csdn.net/sky_monkey/article/details/9495571转载于:https://www.cnblogs.com/eustoma/p/4217028.html

lammps软件_Lammps模型构建的方法之一:组合模型构建

对于Lammps初学者&#xff0c;建模的方法主要有以下几种&#xff1a;1、在Lammps中自行建模&#xff0c;适合金属等简单的模型&#xff0c;如果遇到聚合物就比较麻烦了&#xff1b;2、通过第三方软件建模&#xff0c;例如&#xff1a;Matlab、Python、VMD、Material Studio(MS)…

MapReduce 编程实践:统计对象中的某些属性

文章目录1. 生成数据2. 编写实体类3. Mapper类4. Reducer类5. Driver类6. 运行参考书&#xff1a;《Hadoop大数据原理与应用》 相关文章&#xff1a;MapReduce 编程实践 1. 生成数据 超市消费者 数据&#xff1a; id&#xff0c; 时间&#xff0c;消费金额&#xff0c;会员/…

linux共享数据,使用Linux共享数据对象

Linux共享数据对象类似于windows中的动态链接库&#xff0c;其后缀通常为so.* (*为版本号)&#xff0c;例如为我们所熟知的libpcap&#xff0c;它对应的文件为/usr/lib/libpcap.so。如果程序中使用了某共享数据对象文件&#xff0c;需要在链接时指定gcc的链接参数。如使用libpc…

pythonselenium提高爬虫效率_[编程经验] Python中使用selenium进行动态爬虫

Hello&#xff0c;大家好&#xff01;停更了这么久&#xff0c;中间发生了很多事情&#xff0c;我的心情也发生了很大的变化&#xff0c;看着每天在增长的粉丝&#xff0c;实在不想就这么放弃了&#xff0c;所以以后我会尽量保持在一周一篇的进度&#xff0c;与大家分享我的学习…

超级签名源码_企业签名和超级签名有哪些区别?

我们知道iOS系统对于非App Store中的应用是有安装限制的&#xff0c;而App Store严格的审核机制又将许多APP拒之门外&#xff0c;这令不少开发者们郁闷不已。所以很多开发者们会选择苹果签名的方式&#xff0c;让自己的iOS APP可以不经过App Store就安装在用户的苹果手机上&…

2015-01-14

1.鞋子到了 2.网络一天没有好 3. 又吸烟了,难受 4. 单双杠&#xff1a;60 5. 洗澡&#xff1a;no 6. 仰卧起坐&#xff1a;100 7. 洗脚/刷牙 8.曾的车 9.老梁关世界 总结&#xff1a;今天还好吧&#xff0c;但我还是很想znn&#xff01;&#xff01; 转载于:https://www.cnblo…

天池 在线编程 到达终点

文章目录1. 题目2. 解题1. 题目 描述 A robot is located in a pair of integer coordinates (x, y). It must be moved to a location with another set of coordinates. Though the bot can move any number of times, it can only make the following two types of moves:…

python os函数_python os模块主要函数

使用python提供的os模块&#xff0c;对文件和目录进行操作&#xff0c;重命名文件&#xff0c;添加&#xff0c;删除&#xff0c;复制目录以及文件等。一、文件目录常用函数在进行文件和目录操作时&#xff0c;一般会用到以下几种操作。1、获得当前&#xff1b;路径在python中可…

第十七节(is-a 、is-like-a 、has-a,包和 import )

is - a 类与类之间的继承关系&#xff1b;is - like - a 类与接口之间的关系&#xff1b;has - a 关联关系&#xff1b; public class Animal{public void method01();}// 类与类之间的关系class Dog extends Animal{ // Dog is a Animal} /// public interface I{public void…

quartz获取开始结束时间_Springboot集成quartz

Quartz 是一个完全由 Java 编写的开源作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制。本文描述在springboot 2.x环境下怎么集成quartz。一、添加quartz到项目中在pom.xml中加入 <dependency>特别注意application入口类的注解&#xff0c;这里一定…

linux 添加本地磁盘,XenServer如何添加本地存储

在一次测试中&#xff0c;发现本地有两块磁盘&#xff0c;但是只有一块磁盘在XenServer中显示出来&#xff0c;另外一块没有显示。本地只有一个Local storage。查询KB后&#xff0c;发现XenServer可以添加多块本地存储。详情&#xff0c;请见KB&#xff1a;CTX121313详细添加如…

流畅的Python 5. 函数

文章目录1. 函数对象2. 高阶函数3. 匿名函数4. 可调用函数5. 定位参数、仅限关键字参数6. 获取参数信息7. 函数注解8. 支持函数式编程的包1. 函数对象 def factorial(n):returns n! n的阶乘return 1 if n < 2 else n * factorial(n - 1)print(factorial(42)) print(factori…

python方向键键值_python字典键值对的添加和遍历方法

添加键值对 首先定义一个空字典 >>> dic{} 直接对字典中不存在的key进行赋值来添加 >>> dic[name]zhangsan >>> dic {name: zhangsan} 如果key或value都是变量也可以用这种方法 >>> keyage >>> value30 >>> dic[key]val…