コンテンツ プロバイダ
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
コンテンツ プロバイダは、アプリ自体が保存するデータまたは他のアプリによって保存されたデータへのアクセスをアプリ側で管理し、他のアプリとデータを共有する方法を提供します。これらはデータをカプセル化し、データ セキュリティを定義するメカニズムを提供します。コンテンツ プロバイダは、あるプロセス内のデータと別のプロセスで実行されているコードを接続するための標準的なインターフェースです。
コンテンツ プロバイダを実装すると、多くの利点があります。最も重要な点は、図 1 に示すように、他のアプリが安全にアプリデータにアクセスして変更できるように、コンテンツ プロバイダを構成できることです。
図 1. コンテンツ プロバイダがストレージへのアクセスを管理する方法を示す概要図。
データを共有する予定がある場合は、コンテンツ プロバイダを使用してください。データを共有する予定がない場合は、使用する必要はありません。ただし、抽象化により、データへのアクセスに依存する他のアプリに影響を与えずに、アプリのデータ ストレージの実装を変更できるという利点もあります。
このシナリオでは、コンテンツ プロバイダのみが影響を受け、コンテンツ プロバイダにアクセスするアプリは影響を受けません。たとえば、図 2 に示すように、SQLite データベースを代替ストレージと交換できます。
図 2. コンテンツ プロバイダ ストレージの移行のイラスト。
他の多くのクラスは ContentProvider
クラスに依存しています。
これらのクラスのいずれかを使用する場合は、アプリにコンテンツ プロバイダを実装する必要があります。同期アダプター フレームワークを使用する場合は、代わりにスタブ コンテンツ プロバイダを作成することもできます。詳細については、スタブ コンテンツ プロバイダを作成するをご覧ください。また、次のような場合は、独自のコンテンツ プロバイダが必要になります。
- カスタム検索候補をアプリに実装するため。
- アプリデータをウィジェットに公開するため
- アプリケーションから複雑なデータやファイルをコピーして別のアプリケーションに貼り付ける場合。
Android フレームワークには、音声、動画、画像、個人の連絡先情報などのデータを管理するコンテンツ プロバイダが含まれています。その一部は、android.provider
パッケージのリファレンス ドキュメントに記載されています。一定の制限のもと、これらのプロバイダはすべての Android アプリからアクセスできます。
コンテンツ プロバイダを使用すると、SQLite リレーショナル データベースなどの構造化データや、画像ファイルなどの非構造化データの両方を含む、さまざまなデータ ストレージ ソースへのアクセスを管理できます。Android で利用可能なストレージのタイプの詳細については、データ ストレージとファイル ストレージの概要および
データ ストレージを設計するをご覧ください。
コンテンツ プロバイダのメリット
コンテンツ プロバイダは、データへのアクセス権限をきめ細かく制御できます。アクセスをアプリ内のコンテンツ プロバイダのみに制限すること、他のアプリのデータにアクセスするための包括的な権限を付与すること、データの読み取りと書き込みに異なる権限を設定することを選択できます。コンテンツ プロバイダを安全に使用する方法については、
データ ストレージのセキュリティに関するヒントと
コンテンツ プロバイダの権限をご覧ください。
コンテンツ プロバイダを使用すると、アプリ内のさまざまなデータソースへのアクセスに関する詳細を抽象化できます。たとえば、アプリは構造化レコードを SQLite データベースに保存するだけでなく、動画ファイルや音声ファイルにも格納します。コンテンツ プロバイダを使用して、このようなデータのすべてにアクセスできます。
また、CursorLoader
オブジェクトはコンテンツ プロバイダを利用して非同期クエリを実行し、その結果をアプリの UI レイヤに返します。CursorLoader
を使用してバックグラウンドでデータを読み込む方法について詳しくは、
ローダをご覧ください。
以下のトピックでは、コンテンツ プロバイダについて詳しく説明します。
-
コンテンツ プロバイダの基本
-
既存のコンテンツ プロバイダを使用してデータにアクセスし、更新する方法。
-
コンテンツ プロバイダを作成する
- 独自のコンテンツ プロバイダを設計して実装する方法。
-
カレンダー プロバイダの概要
-
Android プラットフォームの一部であるカレンダー プロバイダにアクセスする方法。
-
連絡先プロバイダ
-
Android プラットフォームの一部である連絡先プロバイダにアクセスする方法。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。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,["# Content providers can help an application manage access to data stored by itself or\nstored by other apps and provide a way to share data with other apps. They encapsulate the\ndata and provide mechanisms for defining data security. Content providers are the standard\ninterface that connects data in one process with code running in another process.\n\n\nImplementing a content provider has many advantages. Most importantly, you can configure a\ncontent provider to let other applications securely access and modify your app data,\nas illustrated in figure 1.\n\n\n**Figure 1.** Overview diagram of how content providers\nmanage access to storage.\n\n\nUse content providers if you plan to share data. If you don't plan to share data,\nyou don't have to use them, but you might choose to because they provide an abstraction\nthat lets you make modifications to your application data storage\nimplementation without affecting other applications that rely on access to your data.\n\n\nIn this scenario, only your content provider is affected and not the applications that\naccess it. For example, you might swap out a SQLite database for alternative storage, as\nillustrated in figure 2.\n\n\n**Figure 2.** Illustration of migrating content provider storage.\n\n\nA number of other classes rely on the [ContentProvider](/reference/android/content/ContentProvider) class:\n\n- [AbstractThreadedSyncAdapter](/reference/android/content/AbstractThreadedSyncAdapter)\n- [CursorAdapter](/reference/android/widget/CursorAdapter)\n- [CursorLoader](/reference/android/content/CursorLoader)\n\n\nIf you use any of these classes, you need to implement a content provider\nin your application. When working with the sync adapter framework you can also create\na stub content provider as an alternative. For more information, see\n[Create a stub content\nprovider](/training/sync-adapters/creating-stub-provider). In addition, you need your own content provider in the following cases:\n\n- To implement custom search suggestions in your application.\n- To expose your application data to widgets.\n- To copy and paste complex data or files from your application to other applications.\n\n\nThe Android framework includes content providers that manage data such as audio, video, images,\nand personal contact information. You can see some of them listed in the reference\ndocumentation for the\n[android.provider](/reference/android/provider/package-summary)`\n` package. With some restrictions, these providers are accessible to any Android\napplication.\n\n\nA content provider can be used to manage access to a variety of data storage sources, including\nboth structured data, such as a SQLite relational database, or unstructured data such as image\nfiles. For more information about the types of storage available on Android, see the\n[Data and file storage overview](/guide/topics/data/data-storage) and\n[Design data storage](/guide/topics/providers/content-provider-creating#DataStorage).\n\nAdvantages of content providers\n-------------------------------\n\n\nContent providers offer granular control over the permissions for accessing data. You can\nchoose to restrict access to only a content provider that is within your application, grant\nblanket permission to access data from other applications, or configure different permissions\nfor reading and writing data. For more information about using content providers securely, see the\n[security tips for data storage](/privacy-and-security/security-tips#StoringData) and\n[Content provider permissions](/guide/topics/providers/content-provider-basics#Permissions).\n\n\nYou can use a content provider to abstract away the details for accessing different data\nsources in your application. For example, your application might store structured records in a\nSQLite database, as well as video and audio files. You can use a content provider to access all\nof this data.\n\n\nAlso, [CursorLoader](/reference/android/content/CursorLoader) objects rely on content providers to run\nasynchronous queries and then return the results to the UI layer in your application. For more\ninformation about using a `CursorLoader` to load data in the background, see\n[Loaders](/training/load-data-background/setup-loader).\n\n\nThe following topics describe content providers in more detail:\n\n\n**[Content provider basics](/guide/topics/providers/content-provider-basics)**\n:\n How to access and update data using an existing content provider.\n\n\n**[Create a content provider](/guide/topics/providers/content-provider-creating)**\n:\n How to design and implement your own content provider.\n\n\n**[Calendar provider overview](/guide/topics/providers/calendar-provider)**\n:\n How to access the Calendar Provider that is part of the Android platform.\n\n\n**[Contacts Provider](/guide/topics/providers/contacts-provider)**\n:\n How to access the Contacts Provider that is part of the Android platform."]]