美文网首页
Android文件存储

Android文件存储

作者: 我想成为创业者 | 来源:发表于2017-06-21 14:47 被阅读0次
网上找的截图

由图可以看出
Environment.getExternalStorageDirectory()方法存储6.0以后需要用户授权,在该方法下app卸载之后数据还保留在SDCard中,留下了垃圾数据。

Context.getExternalFilesDir()Context.getExternalCacheDir()getFilesDir()getCacheDir()相同点:

app卸载后,目录下的数据都会被清空。

Context.getExternalFilesDir()Context.getExternalCacheDir()getFilesDir()getCacheDir()不同点:

  1. 目录的路径不同:前两个的目录在外部SD卡上。后两个的目录在app内部存储上。
  1. 前两个的路径在手机里可以直接看到。后两个的路径需要root以后,用Root Explorer 文件管理器才能看到。
  2. Context.getExternalFilesDir()和getFilesDir()用来存储一些长时间保存的数据(手机助手在清理垃圾时不会清理掉),Context.getExternalCacheDir()、getCacheDir()用来存储一些临时缓存数据(手机助手在清理垃圾时能清理掉)
/**获取app缓存路径
*/
publicStringgetCachePath(Contextcontext) {
    String  cachePath;
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) {
        cachePath = context.getExternalCacheDir().getPath();//外部存储可用
    }else{
        cachePath = context.getCacheDir().getPath();//外部存储不可用
    }
    returncachePath;
}

个人遇到的问题

  • 在版本更新是使用getFilesDir或getCacheDir为存储路径出现解析包错误问题,解决办法
//修改apk权限
public static voidchmod(Stringpermission,Stringpath) {
  try{
    String command ="chmod "+ permission +" "+ path;
    Runtime runtime =Runtime.getRuntime();
    runtime.exec(command);
    }catch(IOExceptione) {
     e.printStackTrace();
   }
}

private void installApk() {
    chmod("777",apkFile.getPath());
    Intent intent =new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(Uri.fromFile(apkFile),"application/vnd.android.package-archive");
    startActivity(intent);
}
  • 使用getCacheDir保存数据,但是用户反馈第二天数据丢失
    原因:用户安装了手机助手,开启了定时清理垃圾数据,使用Context.getExternalCacheDir()getCacheDir()保存的数据会被认为是垃圾然后被清理,应使用Context.getExternalFilesDir()getFilesDir()来存储一些长时间保存的数据

相关文章

  • Android - 文件系统与Android11 分区存储

    *认识Android文件系统 *了解分区存储 *分区存储的适配 一、Android文件系统 Android文件系统...

  • Android数据持久化之文件存储

    参考:Android 中的文件操作Android文件存储总结存储选项-AndroidDevelopers 一. 文...

  • android存储

    Android中的存储 参考 彻底搞懂Android文件存储---内部存储,外部存储以及各种存储路径解惑[http...

  • Android storage

    Android存储结构android中的文件操作详解以及内部存储和外部存储彻底理解android中的内部存储与外部...

  • android基础-文件存储目录

    知识点 android常见的文件保存路径的意义 android开发中常用文件存储相关api 文件存储分类 在如今的...

  • Android文件存储总结

    存储路径及演化 首先看这张文件从Android文件存储使用参考转载的存储结构图,里面明确了通过各种Android接...

  • Android数据存储(三)

    前面两篇文章Android数据存储(一)和Android数据存储(二)分别使用文件存储、SharedPrefere...

  • Room使用

    Android 应用数据存储简单来说有这么几种: 文件存储 SharedPreference 存储 SQLite ...

  • 小知识点(二)android的数据存储与mysql

    Android存储分为三类:学习笔记| AS入门(七) 数据存储篇 - 简书 一.文件存储 在Android中写入...

  • Android存储方式之文件存储数据

    文件存储数据是指在Android系统中,直接把数据以文件的形式存储在设备中。Android系统提供了openFil...

网友评论

      本文标题:Android文件存储

      本文链接:https://www.haomeiwen.com/subject/qbxtcxtx.html