只要是需要进行联网获取数据的APP,都会在本地产生缓存文件。那么,这些缓存文件到底放在什地方合适呢?系统有没有给我们提供建议的缓存位置呢?不同的缓存位置有什么不同呢?

考虑到卸载APP必须删除缓存

在Android手机里面,缓存的位置分为两类,一类是Internal Storage,另外一类是External Storage,

Internal Storage(内部存储)

   你的app的internal storage 目录是以你的app的包名作为标识存放在Android文件系统的特定目录下[data/data/com.example.xx]

  若是你没有设置为可读或者可写,其他app是没有办法读写的。因此只要你使用MODE_PRIVATE ,那么这些文件就不可能被其他app所访问

  内部存储在你的APP卸载的时候,会一块被删除

  我们可以在cache目录里面放置我们的图片缓存,而且cache与files的差别在于,如果手机的内部存储控件不够了,会自行选择cache目录进行删除,因此,不要把重  要的文件放在cache文件里面

  1. File file1 = new File(getFilesDir(), "getFilesDir.txt");
  2. Log.d("TAG", "file1=" + file1.getAbsolutePath());
  3. try {
  4. OutputStream outputStream1 = new FileOutputStream(file1);
  5. outputStream1.write("file".getBytes());
  6. outputStream1.close();
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. }

  运行结果

    1. 02-03 07:18:04.068  22237-22237/? D/TAG﹕ file1=/data/data/com.socks.baidudemo/files/getFilesDir.txt

  

  1. File file2 = new File(getCacheDir(), "cache.txt");
  2. Log.d("TAG", "file2=" + file2.getAbsolutePath());
  3. try {
  4. OutputStream outputStream1 = new FileOutputStream(file2);
  5. outputStream1.write("cache".getBytes());
  6. outputStream1.close();
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. }

    运行结果

      02-03 07:19:31.508  23652-23652/? D/TAG﹕ file2=/data/data/com.socks.baidudemo/cache/cache.txt

  以下是运行后的效果

Internal Storage(外部存储)

  并不总是可用的,因为用户可以选择把这部分作为USB存储模式,这样就不可以访问了。
    是大家都可以访问的,因此保存到这里的文件是失去访问控制权限的。
    当用户卸载你的app时,系统仅仅会删除external根目录(getExternalFilesDir())下的相关文件。
    External是在你不需要严格的访问权限并且你希望这些文件能够被其他app所共享或者是允许用户通过电脑访问时的最佳存储区域。

  1.首先要检查状态

  1. /* Checks if external storage is available for read and write */
  2. public boolean isExternalStorageWritable() {
  3. String state = Environment.getExternalStorageState();
  4. if (Environment.MEDIA_MOUNTED.equals(state)) {
  5. return true;
  6. }
  7. return false;
  8. }
  9. /* Checks if external storage is available to at least read */
  10. public boolean isExternalStorageReadable() {
  11. String state = Environment.getExternalStorageState();
  12. if (Environment.MEDIA_MOUNTED.equals(state) ||
  13. Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
  14. return true;
  15. }
  16. return false;
  17. }

  2.外部私有存储

  1. File file3 = new File(getExternalCacheDir().getAbsolutePath(), "getExternalCacheDir.txt");
  2. try {
  3. OutputStream outputStream1 = new FileOutputStream(file3);
  4. outputStream1.write("getExternalCacheDir".getBytes());
  5. outputStream1.close();
  6. } catch (Exception e) {
  7. e.printStackTrace();
  8. }
  9. Log.d("TAG", "file3=" + file3);
  10. File file4 = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "getExternalFilesDir.txt");
  11. try {
  12. OutputStream outputStream1 = new FileOutputStream(file4);
  13. outputStream1.write("getExternalFilesDir".getBytes());
  14. outputStream1.close();
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }

效果图

  

  3外部公共存储

  APP产生的文件不需要隐藏,即对用户是可见的,那么你可以把文件放在外部的公共存储文件下面

  1. Environment.getExternalStorageDirectory()
  2. Environment.getExternalStoragePublicDirectory()

最新文章

  1. Source Insight常用功能设置
  2. 【Ajax 基础学习】
  3. Yii源码阅读笔记(三十二)
  4. Session 类
  5. JavaScript为input/textarea自定义hover,focus效果
  6. mkdir递归创建目录
  7. iOS中计算文件夹中文件大小
  8. 【Stage3D学习笔记续】真正的3D世界(四):空间大战雏形
  9. 分治算法求乘方a^b 取余p(divide and conquer)
  10. (视频)《高速创建站点》 4.2 完结篇 – 应用运营vs.发射卫星,遥測(Telemetry) 技术
  11. php中serialize、unserialize与json_encode、json_decode比较
  12. Jackson注解学习参考(转)
  13. Modbus通信协议的压力测试
  14. SVProgressHUD源码解读(2.0.3)
  15. JavaScript之中级教程关键
  16. python locust 性能测试:HttpSession
  17. ds18b20驱动及应用程序
  18. MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(一)
  19. CSS2.1SPEC:视觉格式化模型之width属性详解(下)
  20. [ASP.NET 大牛之路]03 - C#高级知识点概要(2) - 线程和并发

热门文章

  1. ORM框架为什么不流行了
  2. 一款基jquery超炫的动画导航菜单
  3. div 背景自适应
  4. vmware克隆Centos6.4虚拟机网卡无法启动问题
  5. iOS边练边学--定时任务和HUD
  6. 没有启动 ASP.NET State service错误的解决方法
  7. Python python的输入输出
  8. The configuration file 'appsettings.json' was not found and is not optional
  9. perl chomp 函数的真正作用
  10. SVN版本冲突,导致出现Files 的值“ < < < < < < < .mine”无效。路径中具有非法字符。