# 应用数据与文件

Android 使用与其他平台上基于磁盘的文件系统类似的文件系统。系统为保存应用数据提供了几种选项：

* **应用专用存储** - 用于保存其他应用不应访问的敏感信息的内部存储。
* **共享存储** - 用于应用与其他应用共享的文件，包括媒体、文档和其他文件。
* **首选项** - 用于在键值对中存储私有、原始数据的存储。
* **数据库** - 使用 Room 持久性库在私有数据库中存储结构化数据。

|                                                                                       | 内容类型                 | 访问方法                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       | 所需权限                                                                                                                                                                                                                                                                    |
| ------------------------------------------------------------------------------------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [应用专用文件](https://developer.android.com/training/data-storage/app-specific)            | 应用专用文件               | <p>从内部存储，使用 <a href="https://developer.android.com/reference/android/content/Context#getFilesDir%28%29">getFilesDir()</a> 和 <a href="https://developer.android.com/reference/android/content/Context#getCacheDir%28%29">getCacheDir()</a></p><p>从外部存储，使用 <a href="https://developer.android.com/reference/android/content/Context#getExternalFilesDir%28java.lang.String%29">getExternalFilesDir()</a> 和 <a href="https://developer.android.com/reference/android/content/Context#getExternalCacheDir%28%29">getExternalCacheDir()</a></p> | <p>内部存储永远不需要权限</p><p>当应用在运行 Android 4.4（API 级别 19）或更高版本的设备上使用时，外部存储不需要权限</p>                                                                                                                                                                                            |
| [媒体](https://developer.android.com/training/data-storage/shared/media)                | 可共享的媒体文件（图像、音频文件、视频） | [MediaStore](https://developer.android.com/reference/android/provider/MediaStore) API                                                                                                                                                                                                                                                                                                                                                                                                                                                      | <p>在 Android 11（API 级别 30）或更高版本上访问其他应用的文件时需要 <strong>READ\_EXTERNAL\_STORAGE</strong></p><p>在 Android 10（API 级别 29）上访问其他应用的文件时需要 <strong>READ\_EXTERNAL\_STORAGE</strong> 或 <strong>WRITE\_EXTERNAL\_STORAGE</strong></p><p>在 Android 9（API 级别 28）或更低版本上访问所有文件都需要权限</p> |
| [文档和其他文件](https://developer.android.com/training/data-storage/shared/documents-files) | 其他类型的可共享内容，包括下载的文件   | 存储访问框架 (Storage Access Framework)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          | 无                                                                                                                                                                                                                                                                       |
| [应用首选项](https://developer.android.com/training/data-storage/shared-preferences)       | 键值对                  | [Jetpack Preferences](https://developer.android.com/guide/topics/ui/settings/use-saved-values) 库                                                                                                                                                                                                                                                                                                                                                                                                                                           | 无                                                                                                                                                                                                                                                                       |
| 数据库                                                                                   | 结构化数据                | [Room](https://developer.android.com/training/data-storage/room) 持久性库                                                                                                                                                                                                                                                                                                                                                                                                                                                                      | 无                                                                                                                                                                                                                                                                       |

## 参考资料

* [Android 开发者：数据和文件存储概览](https://developer.android.com/training/data-storage)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.cdxiaodong.life/android-ying-yong-an-quan/gai-shu/app-data-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
