android 存储
First introduced in Android 10, scoped storage is designed to protect app and user data and reduce file clutter. Since then, you’ve provided a lot of valuable feedback, which has helped us evolve the feature — thank you. Android 11 includes several notable enhancements that are based on your feedback. For example, we’ve enabled direct file path access to media files to improve compatibility of existing code and libraries. We understand that many apps, especially complex ones like Viber, require thoughtful planning to adopt scoped storage in order to continue supporting existing users, ensure adherence to current storage best practices, and maintain backward compatibility. Based on conversations with developers and lively discussions on public forums, we’ve prepared an FAQ to help you better understand various capabilities, behavior changes, and restrictions in scoped storage.
范围存储首先在Android 10中引入,旨在保护应用程序和用户数据并减少文件混乱。 从那时起,您提供了很多有价值的反馈,这些反馈已帮助我们改进了此功能-谢谢。 Android 11根据您的反馈进行了几项显着的增强。 例如,我们已启用对媒体文件的直接文件路径访问 ,以改善现有代码和库的兼容性。 我们了解到,许多应用程序,特别是Viber等复杂的应用程序,需要进行周密的计划以采用范围存储,以便继续支持现有用户,确保遵循当前的存储最佳实践并保持向后兼容性。 基于与开发人员的对话和在公共论坛上的活跃讨论,我们准备了一个FAQ,以帮助您更好地了解范围内存储的各种功能,行为更改和限制。
常问问题 (FAQ)
例如,范围存储是否允许应用使用文件API访问具有文件路径的文件? (Does Scoped Storage allow apps to access files with file paths, using File API, for example?)
We recognize that some apps rely on code or libraries that access media file paths directly. Therefore on Android 11, apps with the read external storage permission are able to access files with file paths in the scoped storage environment. On Android 10 devices, this is not available to apps in the scoped storage environment unless they have opted-out by setting the android:requestLegacyExternalStorage manifest attribute. To ensure continuity across Android versions, if your app targets Android 10 or above, you should also opt-out. See scoped storage best practices for details.
我们认识到某些应用程序依赖直接访问媒体文件路径的代码或库。 因此,在Android 11上,具有读取外部存储权限的应用程序可以访问作用域存储环境中具有文件路径的文件。 在Android 10设备上,除非它们通过设置android:requestLegacyExternalStorage清单属性选择退出 ,否则此设置不适用于作用域存储环境中的应用程序。 为确保Android版本之间的连续性,如果您的应用定位到Android 10或更高版本,则还应选择退出。 有关详细信息,请参见作用域存储最佳实践 。
文件路径访问的性能与Media Store API相比如何? (How does the performance of file path access compare to Media Store APIs?)
- The performance really depends on the exact use case. For sequential reads like in the case of playback of videos, file path access offers comparable performance to Media Store. However for random reads and writes, using file path can be up to twice as slow. For the fastest and most consistent read and writes we recommend Media Store APIs. 性能实际上取决于确切的用例。 对于像视频播放一样的顺序读取,文件路径访问可提供与Media Store相当的性能。 但是,对于随机读取和写入,使用文件路径的速度最多可以慢一倍。 为了获得最快和最一致的读写,我们建议使用Media Store API。
我的应用需要广泛访问共享存储。 Storage Access Framework是唯一可用的选项吗? (My app needs broad access to shared storage. Is Storage Access Framework the only option available?)
Storage Access Framework (SAF) is indeed one option that allows the user to grant access to directories and files. However, note that there are access restrictions to certain directories, such as the root and Android/data directories. While the majority of apps that need storage access can use best practices such as SAF or Media Store API, there could be cases where apps need broad access to shared storage or can’t do so efficiently with these best practices. For these cases, we have added the MANAGE_EXTERNAL_STORAGE permission to give access to all files on external storage, except the Android/data and Android/obb directories. To learn more about related Google Play guidelines, read the updated policy from the Policy Help Center.
实际上,存储访问框架(SAF)是允许用户授予对目录和文件的访问权限的一种选择。 但是,请注意,对某些目录(例如根目录和Android / data目录)有访问限制 。 尽管大多数需要存储访问的应用程序可以使用最佳实践,例如SAF或Media Store API,但在某些情况下,应用程序需要广泛访问共享存储,或者无法使用这些最佳实践有效地做到这一点。 对于这些情况,我们添加了MANAGE_EXTERNAL_STORAGE权限,以访问外部存储上的所有文件,但Android / data和Android / obb目录除外。 要了解有关Google Play指南的更多信息,请阅读“政策帮助中心”中更新后的政策。
哪些类别的应用程序应请求MANAGE_EXTERNAL_STORAGE权限 ? (What categories of apps should request the MANAGE_EXTERNAL_STORAGE permission?)
- The MANAGE_EXTERNAL_STORAGE permission is intended for apps that have a core use case that requires broad access of files on a device, but cannot do so efficiently using scoped storage best practices. While it isn’t practical to enumerate all possible use cases, some use cases include file managers, backup and restore, anti-virus apps or productivity file editing apps. MANAGE_EXTERNAL_STORAGE权限适用于具有核心用例的应用,这些用例需要对设备上的文件进行广泛访问,但不能使用范围内的存储最佳实践来高效地进行操作。 虽然列举所有可能的用例并不切实际,但一些用例包括文件管理器,备份和还原,防病毒应用程序或生产力文件编辑应用程序。
使用Storage Access Framework是否需要Google Play政策批准? (Does using Storage Access Framework require Google Play policy approval?)
- The Storage Access Framework has been in the platform since Android 4.4. Accessing files via Storage Access Framework gives users better control because the user is involved in picking files and it doesn’t require any user permissions. There’s no Google Play policy related to its usage. 自Android 4.4起,存储访问框架就已经存在于平台中。 通过Storage Access Framework访问文件为用户提供了更好的控制权,因为用户参与了选择文件,并且不需要任何用户权限。 没有与之相关的Google Play政策。
与Android 10相比,在Android 11中使用存储访问框架是否还有其他限制? (Are there any further restrictions to using Storage Access Framework in Android 11 as compared to Android 10?)
Apps that target Android 11 (API level 30) and use Storage Access Framework will no longer be able to grant access to directories, such as the root directory of the SD card and the Download directory. Regardless of target SDK, Storage Access Framework on Android 11 cannot be used to gain access to Android/data and Android/obb directories. Learn more about these restrictions and ways to test the behaviors.
面向Android 11(API级别30)并使用Storage Access Framework的应用将不再能够授予目录访问权限,例如SD卡的根目录和Download目录。 无论目标SDK是什么,Android 11上的Storage Access Framework都不能用于访问Android / data和Android / obb目录。 详细了解这些限制和测试行为的方法。
应用程序如何测试范围存储的变化? (How can apps test out Scoped Storage changes?)
Apps can test out scoped storage behavior related to direct file path access or Media Store APIs via these compatibility flags. There’s also another compatibility flag to test the restrictions to access certain paths with Storage Access Framework.
应用程序可以通过这些兼容性标志来测试与直接文件路径访问或Media Store API相关的范围存储行为。 还有一个兼容性标志可以测试使用Storage Access Framework访问某些路径的限制。
范围存储中的应用程序是否仅限于将文件写入其特定于应用程序的数据目录中? (Are apps in scoped storage limited to writing files into their app-specific data directories?)
In scoped storage, apps can contribute media files to Media Store collections. Media Store will put the files into well organized folders like DCIM, Movies, Download, and so on based on file type. For all such files, apps can also continue to have access via File APIs as well. The OS maintains a system to attribute an app to each media store file, so apps can read/write files that they originally contributed to the Media Store without needing storage permissions.
在范围存储中,应用程序可以将媒体文件贡献给Media Store集合。 Media Store会根据文件类型将文件放入组织良好的文件夹中,例如DCIM,Movies,Download等。 对于所有此类文件,应用程序也可以继续通过文件API进行访问。 操作系统维护一个将应用程序归因于每个媒体存储文件的系统,因此应用程序可以读取/写入最初添加到媒体存储中的文件,而无需存储权限。
由于已弃用“媒体存储数据”列 ,有关使用该列的指导是什么? (What is the guidance around using the Media Store DATA column since it’s been deprecated?)
- On Android 10, apps in the scoped storage environment cannot access files using the file path. To be consistent with this design, we deprecated the DATA column then. Based on your feedback on the needs to work with existing native code or libraries, Android 11 now supports file path access for apps in scoped storage. Accordingly, the DATA column could actually be useful for some scenarios. For inserts and updates into the Media Store, apps in Scoped Storage should use DISPLAY_NAME and RELATIVE_PATH columns. They can no longer use the DATA column for this. When reading Media Store entries for files that exist on disk, the DATA column will have a valid file path, which can be used with the File API or NDK file libraries. Apps should however be prepared to handle any file I/O errors from these operations and should not assume the file is always available. 在Android 10上,作用域存储环境中的应用程序无法使用文件路径访问文件。 为了与此设计保持一致,我们随后弃用了DATA列。 根据您对使用现有本机代码或库的需求的反馈,Android 11现在支持范围存储中应用程序的文件路径访问。 因此,DATA列在某些情况下实际上可能有用。 为了在媒体存储中插入和更新,作用域存储中的应用应使用DISPLAY_NAME和RELATIVE_PATH列。 他们不能再为此使用DATA列。 当读取磁盘上存在的文件的媒体存储条目时,DATA列将具有有效的文件路径,该路径可与File API或NDK文件库一起使用。 但是,应用程序应准备好处理这些操作中的任何文件I / O错误,并且不应假定文件始终可用。
对于选择退出范围存储的应用程序,何时必须与范围存储兼容? (For apps that have opted out of Scoped Storage, when will they have to be compatible with Scoped Storage?)
- On devices running Android 11 or higher, apps will be put into Scoped Storage as soon as they target Android 11 or higher. 在运行Android 11或更高版本的设备上,应用一旦定位到Android 11或更高版本,便会立即放入“作用域存储”中。
建议使用什么方式迁移当前存储在范围存储之外的数据? (What is the recommended way to migrate data that we currently store outside of Scoped Storage?)
preserveLegacyExternalStorage flag allows an app to retain legacy storage access on upgrades even while targeting Android 11. However beware that on new installs on Android 11, this flag has no effect. Please make code changes to adapt to Scoped Storage before targeting Android 11. Learn more about data migration best practices.
prepareLegacyExternalStorage标志可让应用即使在定位到Android 11的情况下也可以在升级时保留旧版存储访问权限。但是请注意,在Android 11上的新安装中,此标志无效。 在定位到Android 11之前,请进行代码更改以适应范围存储。 了解有关数据迁移最佳做法的更多信息。
鉴于某些软件包安装程序(例如应用商店)需要访问Android / obb目录,因此是否有例外? (Are there any exceptions for Android/obb directories given that some package installers, like app stores, need access to it?)
Apps that hold the REQUEST_INSTALL_PACKAGES permission can access other apps’ Android/obb directories.
拥有REQUEST_INSTALL_PACKAGES权限的应用程序可以访问其他应用程序的Android / obb目录。
We hope you find this FAQ useful in planning your adoption of scoped storage. Please visit our best practice documentation for more information.
我们希望您能找到该常见问题解答,以帮助您计划采用范围存储。 请访问我们的最佳做法文档以获取更多信息。
翻译自: https://medium.com/androiddevelopers/android-11-storage-faq-78cefea52b7c
android 存储