Using the Media Store Provider

The Android Media Store is a managed repository of audio, video, and image files.

Whenever you add a new multimedia file to the file system, it should also be added to the Media Store. This will expose it to other applications, including the default media player. Chapter 11 shows you how to use the Content Scanner to add new media to the Media Store.

To access media from the Media Store, query the image, video, or audio Content Providers using the techniques described earlier within this chapter. The MediaStore class includes Audio, Video, and Images subclasses, which in turn contain subclasses that are used to provide the column names and content URIs for each media provider.

The Media Store segregates media kept on the internal and external volumes of the host device. Each of the Media Store subclasses provides a URI for either the internally or externally stored media using the forms:

> MediaStore.<mediatype>.Media.EXTERNAL_CONTENT_URI

> MediaStore.<mediatype>.Media.INTERNAL_CONTENT_URI

Listing 7-18 shows a simple code snippet used to find the song title and album name for each piece of audio stored on the external volume.

LISTING 7-18: Accessing the Media Store Content Provider

Available for download on Wrox.com

// Get a cursor over every piece of audio on the external volume. Cursor cursor =

getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);

// Let the activity manage the cursor lifecycle. startManagingCursor(cursor);

// Use the convenience properties to get the index of the columns int albumldx = cursor.getColumnlndexOrThrow(MediaStore.Audio.Media.ALBUM);

int titleldx = cursor. getColumnlndexOrThrow(MediaStore.Audio.Media.TITLE);

String[] result = new String[cursor.getCount()]; if (cursor.moveToFirst()) do {

// Extract the song title.

String title = cursor.getString(titleIdx);

// Extract the album name.

String album = cursor.getString(albumIdx);

result[cursor.getPosition()] = title + " (" + album + ")"; } while(cursor.moveToNext());

In Chapter 11 you'll learn how to play audio and video resources stored in the Media Store by specifying the URI of a particular multi media item.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment