設定檔案分享
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如要安全地提供應用程式檔案給其他應用程式,您必須將應用程式設為提供
檔案的安全控制代碼,採內容 URI 格式。Android
FileProvider
元件會產生內容 URI
產生的檔案。本課程將說明如何新增預設值
在應用程式中實作 FileProvider
,以及如何
指定要提供給其他應用程式的檔案
注意:FileProvider
類別屬於
AndroidX 核心程式庫。資訊
如果想在應用程式中加入這個程式庫,請參閱
宣告依附元件。
指定 FileProvider
定義應用程式的 FileProvider
時,您必須在
資訊清單。此項目指定使用產生內容 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
檔案
子目錄在這個檔案中,新增以下項目的 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
您必須將檔案的內容 URI
default_image.jpg
,FileProvider
會傳回
存取 URI:
content://com.example.myapp.fileprovider/myimages/default_image.jpg
如需其他相關資訊,請參閱:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[null,null,["上次更新時間: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)"]]