• Android 内部存储, 外部存储, 移动存储


    内部存储器 介绍

    The Storage Situation: Internal Storage

    简单来说Android的SDK认为内部存储器是一个对于你的app来说特定的, 可以放文件的目录. 这个目录下的文件的app只能由你的app读和写, 其他的app将不能够访问这些文件. 当然root权限例外.

    Context中给你的获取内部存储器位置的方法....(sometimes):

    • getCacheDir()

    • getDir()

    • getDatabasePath()

    • getFilesDir()

    • openFileInput()

    • openFileOutput()

    其他的文件存取操作都依赖于此.例如数据库操作.

    你的app的内部存储一般在/data/data/your.application.package.name

    在其他的情况下

    如果你的程序员这样写:

    File f = new File("/data/data/their.app.package.name/files/foo.txt");

    这样写不仅需要打更多的字, 我们需要的目录还不一定在这里.

    当然, 我们有更好的代码:

    File f=new File(getFilesDir(), "foo.txt");

    在android10中内部存储器的操作还没有发生改变.

    Google's understand of "External Storage"

    The Storage Situation: External Storage

    外部存储器 介绍

    Android SDK文档是这么对"外部存储"定义的:

    Every Android-compatible device supports a shared “external storage” that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer

    官方文档因为历史原因把'the stuff that shows up when the user plugs their device into a computer using a USB cable'这个定义为"external storage"外部存储, 但是Android4.4 又增加了可移动媒体(存储?), 这也下面那篇文章讲的东西.

    这里提及的外部存储的路径都是由Environment.getExternalStorageDirectory().所指定的.但是, 这个方法在Api29(即android10)被弃用了.

    在Android早期的版本, 内部存储和外部存储是相互分割的分区, 但是在3.0之后, 内部存储和外部存储在相同的分区,只是在不同的目录里面, 然后硬件厂商也没有继续把内部存储和外部存储当作两个分离的部分.(应该就是说现在的手机都是买来直接就是64GB, 128GB的, 不在物理上把内部和外部隔离了).

    • For us as developers, the actual path under which this magical “external storage” was accessed varied over the years, from /sdcard, to locations under /storage, to eventually the current /mnt/shell/emulated/0.

    所以说不能把目录之类的东西hardcode进去

    永远不要硬编码路径

    • Environment.getExternalStorageDirectory() 可以获取外部存储的目录just a big basket of random content

    • getExternalFilesDir()和getExternalCacheDir() 获取的是app相对应的在外部存储的目录的两个文件夹.

    • Environment.getExternalStoragePublicDirectory() 集中放置的那些图片, 电影的文件.

    Android规定的app的权限: Android1.5的时候, 有一个WRITE_EXTERNAL_STORAGE的permission, 在打开程序的时候, 回想用户请求app打算修改外部存储的内容, 任何app可以直接读取外部存储.

    在Android4.4之后, 强制执行READ_EXTERNAL_STORAGE, 如果你没有权限你无法读取外部存储.getExternalFilesDir() and getExternalCacheDir()这两个方法甚至无需权限就可以直接读写.

    android10 外部存储也不给随便创建文件夹了, 目前android10可以临时用一种方式来进行以前版本的操作。详见 Temporarily opt-out of scoped storage

    Google Thinks "Removable Storage"

    The Storage Situation: Removable Storage

    1. 在大多数的设备上External Storage ≠ removable storage

    2. 在Android SDK中没有提到移动存储.

    引用的一大段就是说在android4.4之后才有SDcard的支持,应用只能对自己的部分进行读写操作, 且无需权限, 并且通过文件浏览器无需任何权限就可以对任何文件进行操作.

    在此之前和sdcard建立连接是通过各种黑科技.. 硬件厂商一套, 开发者自己又一套..

    4.4之后只能读, 而没有写的权限.

    Google管这个叫secondary external storage

    小结

    总而言之android4.4到android10都可以在外部存储应用程序自己新建一个文件夹,用于备份等必要操作,而不用担心在app卸载的时候被删除。而android10之后需要使用其他的方法进行适配。详见

  • 相关阅读:
    shallow update not allowed
    GH001 on github
    Castle动态代理拦截
    Spring Boot : Whitelabel Error Page解决方案
    Springboot 之 Hibernate自动建表(Mysql)
    hibernate.hbm2ddl.auto配置详解
    【Spring boot】第一个项目 Springboot + mysql + hibernate
    【持久化框架】Mybatis与Hibernate的详细对比
    Hibernate基本原理(一)
    Hibernate各种主键生成策略与配置详解
  • 原文地址:https://www.cnblogs.com/NoitTion/p/12510993.html
Copyright © 2020-2023  润新知