ファイルの共有
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
多くの場合、アプリでは 1 つ以上のファイルを別のアプリに提供する必要があります。たとえば、画像ギャラリーで画像エディタにファイルを提供する場合や、ファイル管理アプリでユーザーが外部ストレージの領域間でファイルをコピーして貼り付けられるようにする場合などです。送信側アプリがファイルを共有する方法の一つとして、受信側アプリからのリクエストに応答する方法があります。
いずれの場合も、アプリから別のアプリにファイルを提供する安全な方法は、受信側のアプリにファイルのコンテンツ URI を送信し、その URI に一時的なアクセス権限を付与することです。
一時的な URI アクセス権限のあるコンテンツ URI は、URI を受け取るアプリにのみ適用され、自動的に期限切れになるため、安全です。Android の FileProvider
コンポーネントには、ファイルのコンテンツ URI を生成するためのメソッド getUriForFile()
が用意されています。
少量のテキストまたは数値データをアプリ間で共有する場合は、そのデータを含む Intent
を送信する必要があります。Intent
を使用してシンプルなデータを送信する方法については、トレーニング クラスのシンプルなデータの共有をご覧ください。
このクラスでは、Android FileProvider
コンポーネントによって生成されたコンテンツ URI と、受信側アプリに付与したコンテンツ URI の一時的な権限を使用して、アプリから別のアプリにファイルを安全に共有する方法について説明します。
レッスン
- ファイル共有のセットアップ
-
ファイルを共有するようにアプリを設定する方法について学びます。
- ファイルの共有
- ファイルのコンテンツ URI を生成し、URI にアクセス権限を付与して、URI をアプリに送信することで、別のアプリにファイルを提供する方法を学習します。
- 共有ファイルのリクエスト
-
別のアプリで共有されているファイルをリクエストし、ファイルのコンテンツ URI を受信して、コンテンツ URI を使用してファイルを開く方法を学習します。
-
ファイル情報の取得
FileProvider
で生成されたコンテンツ URI を使用して、アプリが MIME タイプやファイルサイズなどのファイル情報を取得する方法について学習します。
その他の関連情報については、以下をご覧ください。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。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,["# Sharing files\n\nApps often have a need to offer one or more of their files to another app. For example, an image\ngallery may want to offer files to image editors, or a file management app may want to allow\nusers to copy and paste files between areas in external storage. One way a sending app can\nshare a file is to respond to a request from the receiving app.\n\n\nIn all cases, the only secure way to offer a file from your app to another app is to send the\nreceiving app the file's content URI and grant temporary access permissions to that URI.\nContent URIs with temporary URI access permissions are secure because they apply only to the\napp that receives the URI, and they expire automatically. The Android\n[FileProvider](/reference/androidx/core/content/FileProvider) component provides the method\n[getUriForFile()](/reference/androidx/core/content/FileProvider#getUriForFile(android.content.Context, java.lang.String, java.io.File)) for\ngenerating a file's content URI.\n\n\nIf you want to share small amounts of text or numeric data between apps, you should send an\n[Intent](/reference/android/content/Intent) that contains the data. To learn how to send simple data with an\n[Intent](/reference/android/content/Intent), see the training class\n[Sharing simple data](/training/sharing).\n\n\nThis class explains how to securely share files from your app to another app using content URIs\ngenerated by the Android [FileProvider](/reference/androidx/core/content/FileProvider) component and\ntemporary permissions that you grant to the receiving app for the content URI.\n\nLessons\n-------\n\n**[Setting up file sharing](/training/secure-file-sharing/setup-sharing)**\n:\n Learn how to set up your app to share files.\n\n**[Sharing a file](/training/secure-file-sharing/share-file)**\n:\n Learn how to offer a file to another app by generating a content URI for the file,\n granting access permissions to the URI, and sending the URI to the app.\n\n**[Requesting a shared file](/training/secure-file-sharing/request-file)**\n:\n Learn how to request a file shared by another app, receive the content URI for the file,\n and use the content URI to open the file.\n\n\n**[Retrieving file information](/training/secure-file-sharing/retrieve-info)**\n:\n Learn how an app can use a content URI generated by a\n [FileProvider](/reference/androidx/core/content/FileProvider) to retrieve file information including\n MIME type and file size.\n\nFor additional related information, refer to:\n\n- [Storage Options](/guide/topics/data/data-storage)\n- [Saving Files](/training/basics/data-storage/files)\n- [Sharing Simple Data](/training/sharing)"]]