The following is the first few sections of a chapter from The Busy Coder's Guide to Android Development, plus headings for the remaining major sections, to give you an idea about the content of the chapter.


The MediaStore Provider

Playing back media is a popular pastime on Android devices, one in which your app may want to participate. The easiest way for you to find out what media is available for you to display, edit, or otherwise work with is via the MediaStore content provider. MediaStore is part of the Android framework and allows you to query for images, audio files, and video files that are indexed on the device.

This chapter will review the general workings of MediaStore, plus work through an example of getting video files — and their thumbnails — from MediaStore.

Prerequisites

Understanding this chapter requires that you have read the chapters on:

It is also a pretty good idea to have read the chapters on media recording and playback that might be of relevance, depending on what you intend to do with the MediaStore:

You might also wish to consider skimming through the chapter on files again, as it will be cross-referenced in several places in this chapter.

What Is the MediaStore?

The documentation for MediaStore describes it this way:

The Media provider contains meta data for all available media on both internal and external storage devices.

This definition… leaves a bit to be desired.

From our standpoint as Android developers, the MediaStore is a ContentProvider, supplied by Android. We can use it much like we use other system-supplied providers, like ContactsContract and CalendarContract. In this case, the primary role of MediaStore is for us to find media, just as the primary role of ContactsContract is for us to find contacts.

The “meta data” reference in the documentation refers to the fact that MediaStore itself does not store the media, even though that’s what the name MediaStore would suggest. MediaMetadataStore would be a more accurate description. We can learn about available media — names, durations, etc. — and we can get a Uri from MediaStore pointing to the media, but the media itself lives as a file somewhere else.

Indexed Media

MediaStore has media as a primary focus. Here, “media” refers to:

MediaStore has intrinsic knowledge of these, particularly for the file formats and codecs that Android supports. As a result, the index maintained by MediaStore will contain some metadata in common for all file types, such as:

…and other metadata that will be unique to one or two of the major types, such as:

Indexed Non-Media

As was mentioned in passing in the chapter on files, Android uses MTP for Android 4.0+ as the USB protocol for sharing files with a desktop or notebook computer.

To power this, Android does not go straight to the filesystem, but rather works with MediaStore. MediaStore maintains an index of all files, not just “media”. Whatever shows up in MediaStore is what shows up to the user in their Windows drive letter, macOS mounted volume, etc.

You too can query MediaStore for non-media files. Android will try to maintain a MIME type — probably based on file extensions — and so you can find all indexed PDF files, for example, by querying MediaStore.

MediaStore and “Other” External Storage

The preview of this section was eaten by a grue.

How Does My Content Get Indexed?

The preview of this section is in an invisible, microscopic font.

How Do I Retrieve Video from the MediaStore?

The preview of this section was lost in the sofa cushions.