设置文件分享
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
为了安全地将应用中的文件提供给其他应用,您需要将应用配置为
文件的安全句柄,格式为内容 URI。Android
FileProvider
组件为
文件。本课将向您介绍如何向
FileProvider
的实现,以及如何
指定要提供给其他应用的文件。
注意:FileProvider
类是
AndroidX Core 库。相关信息
如何在应用中添加此库,请参见
声明依赖项。
指定 FileProvider
若要为应用定义 FileProvider
,您需要在
。此条目指定了生成内容 URI 以及生成内容 URI 时要使用的授权,
一个 XML 文件的名称,该文件指定了应用程序可以共享的目录。
以下代码段展示了如何将
<provider>
元素,用于指定
FileProvider
类、相应授权方以及
XML 文件名:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
...>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapp.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
在此示例中,android:authorities
属性指定 URI 授权
您要用于由
FileProvider
。
示例中的授权为 com.example.myapp.fileprovider
。个人专属
应用,请指定由应用的
带有字符串“fileprovider”的 android:package
值。了解详情
授权值,请查看主题
内容 URI 及相关文档
android:authorities
属性。
<meta-data>
子元素
<provider>
指向一个 XML 文件,该文件指定您要
份额。android:resource
属性是该文件的路径和名称,不包含
.xml
扩展名。此文件的内容将在下一部分进行介绍。
将 FileProvider
添加到应用清单后,
您需要指定包含要共享的文件的目录。要指定
目录中,首先在 res/xml/
中创建 filepaths.xml
文件
子目录中在此文件中,通过为
每个目录。以下代码段展示了
res/xml/filepaths.xml
。该代码段还演示了如何共享子目录
(位于内部存储区域中 files/
目录下):
<paths>
<files-path path="images/" name="myimages" />
</paths>
在此示例中,<files-path>
标记共享了
files/
目录中。path
属性
共享 files/
的 images/
子目录。name
属性会告知 FileProvider
添加路径段
将 myimages
设置为 files/images/
子目录中文件的内容 URI。
<paths>
元素可以有多个子元素,每个子元素指定一个不同的
要共享的目录除了 <files-path>
元素之外,您还可以
使用 <external-path>
元素共享外部存储空间中的目录;
<cache-path>
元素,用于共享内部缓存中的目录
目录。如需详细了解指定共享目录的子元素,请参阅
FileProvider
参考文档。
注意:您只能通过 XML 文件指定要
份额;您不能以编程方式添加目录
现在,您已获得 FileProvider
的完整规范。
该函数为应用的 files/
目录中的文件生成内容 URI,
或用于 files/
的子目录中的文件。当您的应用
文件的内容 URI,其中包含使用
<provider>
个元素 (com.example.myapp.fileprovider
),
路径 myimages/
和文件名。
例如,如果您根据FileProvider
代码段,您需要请求该文件的
default_image.jpg
,FileProvider
会返回
以下 URI:
content://com.example.myapp.fileprovider/myimages/default_image.jpg
如需了解其他相关信息,请参阅:
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Setting up file sharing\n\nTo securely offer a file from your app to another app, you need to configure your app to offer\na secure handle to the file, in the form of a content URI. The Android\n[FileProvider](/reference/androidx/core/content/FileProvider) component generates content URIs for\nfiles, based on specifications you provide in XML. This lesson shows you how to add the default\nimplementation of [FileProvider](/reference/androidx/core/content/FileProvider) to your app, and how to\nspecify the files you want to offer to other apps.\n\n\n**Note:** The [FileProvider](/reference/androidx/core/content/FileProvider) class is part of the\n[AndroidX Core Library](/jetpack/androidx/releases/core). For information\nabout including this library in your application, see\n[Declaring dependencies](/jetpack/androidx/releases/core#declaring_dependencies).\n\nSpecify the FileProvider\n------------------------\n\n\nDefining a [FileProvider](/reference/androidx/core/content/FileProvider) for your app requires an entry in\nyour manifest. This entry specifies the authority to use in generating content URIs, as well as\nthe name of an XML file that specifies the directories your app can share.\n\n\nThe following snippet shows you how to add to your manifest the\n[\u003cprovider\u003e](/guide/topics/manifest/provider-element) element that specifies the\n[FileProvider](/reference/androidx/core/content/FileProvider) class, the authority, and the\nXML file name: \n\n```xml\n\u003cmanifest xmlns:android=\"http://schemas.android.com/apk/res/android\"\n package=\"com.example.myapp\"\u003e\n \u003capplication\n ...\u003e\n \u003cprovider\n android:name=\"androidx.core.content.FileProvider\"\n android:authorities=\"com.example.myapp.fileprovider\"\n android:grantUriPermissions=\"true\"\n android:exported=\"false\"\u003e\n \u003cmeta-data\n android:name=\"android.support.FILE_PROVIDER_PATHS\"\n android:resource=\"@xml/filepaths\" /\u003e\n \u003c/provider\u003e\n ...\n \u003c/application\u003e\n\u003c/manifest\u003e\n```\n\n\nIn this example, the [android:authorities](/guide/topics/manifest/provider-element#auth) attribute specifies the URI authority\nthat you want to use for content URIs generated by the\n[FileProvider](/reference/androidx/core/content/FileProvider).\nIn the example, the authority is `com.example.myapp.fileprovider`. For your own\napp, specify an authority consisting of the app's\n[android:package](/guide/topics/manifest/manifest-element#package) value with the string \"fileprovider\" appended to it. To learn more\nabout the authority value, see the topic\n[Content URIs](/guide/topics/providers/content-provider-basics#ContentURIs) and the documentation for the\n[android:authorities](/guide/topics/manifest/provider-element#auth) attribute.\n\n\nThe [\u003cmeta-data\u003e](/guide/topics/manifest/meta-data-element) child element of the\n[\u003cprovider\u003e](/guide/topics/manifest/provider-element) points to an XML file that specifies the directories you want to\nshare. The `android:resource` attribute is the path and name of the file, without\nthe `.xml` extension.The contents of this file are described in the next section.\n\nSpecify sharable directories\n----------------------------\n\n\nOnce you have added the [FileProvider](/reference/androidx/core/content/FileProvider) to your app manifest,\nyou need to specify the directories that contain the files you want to share. To specify the\ndirectories, start by creating the file `filepaths.xml` in the `res/xml/`\nsubdirectory of your project. In this file, specify the directories by adding an XML element for\neach directory. The following snippet shows you an example of the contents of\n`res/xml/filepaths.xml`. The snippet also demonstrates how to share a subdirectory\nof the `files/` directory in your internal storage area: \n\n```xml\n\u003cpaths\u003e\n \u003cfiles-path path=\"images/\" name=\"myimages\" /\u003e\n\u003c/paths\u003e\n```\n\n\nIn this example, the `\u003cfiles-path\u003e` tag shares directories within the\n`files/` directory of your app's internal storage. The `path` attribute\nshares the `images/` subdirectory of `files/`. The `name`\nattribute tells the [FileProvider](/reference/androidx/core/content/FileProvider) to add the path segment\n`myimages` to content URIs for files in the `files/images/` subdirectory.\n\n\nThe `\u003cpaths\u003e` element can have multiple children, each specifying a different\ndirectory to share. In addition to the `\u003cfiles-path\u003e` element, you can\nuse the `\u003cexternal-path\u003e` element to share directories in external storage, and\nthe `\u003ccache-path\u003e` element to share directories in your internal cache\ndirectory. To learn more about the child elements that specify shared directories, see the\n[FileProvider](/reference/androidx/core/content/FileProvider) reference documentation.\n\n\n**Note:** The XML file is the only way you can specify the directories you want to\nshare; you can't programmatically add a directory.\n\n\nYou now have a complete specification of a [FileProvider](/reference/androidx/core/content/FileProvider)\nthat generates content URIs for files in the `files/` directory of your app's\ninternal storage or for files in subdirectories of `files/`. When your app generates\na content URI for a file, it contains the authority specified in the\n[\u003cprovider\u003e](/guide/topics/manifest/provider-element) element (`com.example.myapp.fileprovider`),\nthe path `myimages/`, and the name of the file.\n\n\nFor example, if you define a [FileProvider](/reference/androidx/core/content/FileProvider) according to the\nsnippets in this lesson, and you request a content URI for the file\n`default_image.jpg`, [FileProvider](/reference/androidx/core/content/FileProvider) returns the\nfollowing URI: \n\n```\ncontent://com.example.myapp.fileprovider/myimages/default_image.jpg\n```\n\nFor additional related information, refer to:\n\n- [Storage Options](/guide/topics/data/data-storage)\n- [Saving Files](/training/basics/data-storage/files)"]]