A ContentProvider manages access to a centralized repository of data. In Android, each app runs in its own private sandbox with isolated storage. ContentProviders provide a standardized, secure mechanism for sharing data across process boundaries using standard Content URIs.
Just as a website is identified by an HTTP URL, data inside a ContentProvider is addressed via a Content URI:
content://com.veewom.android.provider/tutorials/42
│ │ │ │
Scheme Authority Path ID
content:// for Android providers..provider).tutorials).Android's system MediaStore provider manages photos, videos, and audio on the device. Here is how an app queries user photos:
import android.content.ContentUris
import android.provider.MediaStore
import android.net.Uri
data class MediaPhoto(val uri: Uri, val displayName: String, val size: Long)
fun queryRecentPhotos(context: Context): List<MediaPhoto> {
val photoList = mutableListOf<MediaPhoto>()
val projection = arrayOf(
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.SIZE
)
val sortOrder = "${MediaStore.Images.Media.DATE_ADDED} DESC"
context.contentResolver.query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
sortOrder
)?.use { cursor ->
val idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID)
val nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME)
val sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE)
while (cursor.moveToNext()) {
val id = cursor.getLong(idColumn)
val name = cursor.getString(nameColumn)
val size = cursor.getLong(sizeColumn)
val contentUri = ContentUris.withAppendedId(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id
)
photoList.add(MediaPhoto(contentUri, name, size))
}
}
return photoList
}
Broaden your foundational and advanced Android application development skills across our curriculum:
How ContentProviders expose SQLite databases across app boundaries.
MediaStore access, READ_MEDIA_IMAGES, and Photo Picker API.
Grant URI read/write permissions via Intent flags.
Encrypt sensitive data prior to storing in database or file system.