An Android ContentProvider is a component that manages access to a structured set of data and exposes it to other apps through content URIs and a ContentResolver. You need one only when data must be shared with other applications or integrated with system features such as search, sync adapters or widgets. For data used only inside your own app, a Room database accessed directly is simpler and safer.
A provider gives you a uniform, permission-controlled interface of query, insert, update, delete and getType, regardless of whether the data lives in SQLite, files or on a network.
Each provider has an authority, a unique string declared in the manifest, and data is addressed by a content URI of the form content://authority/path/id.
| Part | Example | Meaning |
|---|---|---|
| Scheme | content:// | Always the same for providers |
| Authority | com.example.notes.provider | Identifies the provider |
| Path | notes | A table or collection |
| ID | 7 | A single row, optional |
Clients never call the provider directly. They use ContentResolver, which finds the provider by authority, even across processes.
The system exposes many providers, such as the contacts and media stores. Querying looks like this:
Use placeholders and the selection arguments rather than concatenating values into the selection, which prevents SQL injection. Always close the cursor, here with use. Access may require a runtime permission, or on recent versions the photo picker, depending on the data and Android version.
Subclass ContentProvider and implement six methods. This provider exposes a notes table backed by SQLite.
UriMatcher maps URI patterns to constants, setNotificationUri() lets observers refresh when data changes, and getType() returns the MIME type. The update and delete bodies are left minimal here for brevity.
Set android:exported="false" unless other apps need access. When exported, require read and write permissions, and validate every URI and argument, because the caller is untrusted.
The most common provider in everyday apps is FileProvider from AndroidX. It lets you share a file with another app by a content:// URI instead of a file:// path, which has been blocked across apps since Android 7.0 (FileUriExposedException). You declare it in the manifest with a paths XML file, then call FileProvider.getUriForFile() and grant temporary read access with FLAG_GRANT_READ_URI_PERMISSION.
FileProvider or the system share sheet.Write your own provider only for cross-app data sharing or when a framework feature requires it.
No. It is an access layer. The data behind it can be SQLite, files or something else, and clients only see URIs and cursors.
Calls from another process arrive on a binder thread, but calls from your own process can run on the caller's thread. Query from a background dispatcher and keep provider methods thread-safe.
Call notifyChange() in the provider and register a ContentObserver or use a CursorLoader-style flow in the client.
Application.onCreate()?Yes. Providers are created at process start before the application's onCreate(), which is why some libraries initialise through them.
A ContentProvider shares structured data between apps through content URIs and ContentResolver. Implement query, insert, update, delete and getType, declare an authority, and keep it unexported unless sharing is intended. For internal data use Room, and for sharing files use FileProvider.