ファイル共有の設定
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
アプリから別のアプリにファイルを安全に提供するには、
ファイルへの安全なハンドル(コンテンツ URI 形式)。Android
FileProvider
コンポーネントが生成するコンテンツ URI:
XML でユーザーが指定した仕様に基づいて、ファイルが生成されます。このレッスンでは、デフォルトの
アプリへの 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 認証局を指定しています。
生成するコンテンツ URI に使用する URI を
FileProvider
。
この例の認証局は com.example.myapp.fileprovider
です。自分用
権限を指定するには、
android:package
値と文字列「fileprovider」追加します。関連資料
トピックについては、このモジュールの
コンテンツ URI と、
android:authorities
属性。
次の要素の <meta-data>
子要素:
<provider>
は、目的のディレクトリを指定する XML ファイルを指します。
共有します。android:resource
属性は、ファイルのパスと名前です。
.xml
拡張子。このファイルの内容については、次のセクションで説明します。
アプリ マニフェストに FileProvider
を追加したら、次の操作を行います。
共有するファイルが入っているディレクトリを指定する必要があります。Pod の
作成するには、まず 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 であり、コンテンツ 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 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-27 UTC。"],[],[],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)"]]