Media Content Provider in Android with Example

Here is source code of the Program to Demonstrate Media Content Provider in Andorid using Java. The program is successfully compiled and run on a Windows system using Eclipse Ide. The program output is also shown below.

The Android Media Store is a managed repository of audio, video, and image files.Whenever you add a new multimedia file to the filesystem, it should also be added to the Media Store using the Content Scanner, this will expose it to other applications, including media players. In most circumstances it’s not necessary (or recommended) to modify
the contents of the Media Store Content Provider directly. To access the media available within the Media Store, 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 the corresponding media providers.
The Media Store segregates media kept on the internal and external volumes of the host device. Each Media Store subclass provides a URI for either the internally or externally stored media using
the forms:
MediaStore..Media.EXTERNAL_CONTENT_URI
MediaStore..Media.INTERNAL_CONTENT_URI

The following code shows to find the song title and album name for each piece of audio stored on the external volume.

MainActivity.java

package com.example.mediaprovider;
 
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button but = (Button) findViewById(R.id.buttonret);
        but.setOnClickListener(new View.OnClickListener() {
 
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String[] projection = new String[] {
                        MediaStore.Audio.AudioColumns.ALBUM,
                        MediaStore.Audio.AudioColumns.TITLE };
                Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                Cursor cursor = getContentResolver().query(contentUri,
                        projection, null, null, null);
                // Get the index of the columns we need.
                int albumIdx = cursor
                        .getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.ALBUM);
                int titleIdx = cursor
                        .getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.TITLE);
                // Create an array to store the result set.
                String[] result = new String[cursor.getCount()];
                /*
                 * Iterate over the Cursor, extracting each album name and song
                 * title.
                 */
                while (cursor.moveToNext()) {
                    // Extract the song title.
                    String title = cursor.getString(titleIdx);
                    // Extract the album name.
                    String album = cursor.getString(albumIdx);
                    result[cursor.getPosition()] = title + " (" + album + ")";
                }
                // Close the Cursor.
                cursor.close();
            }
 
        });
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

Activity_main.xml

advertisement
advertisement
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
 
    <Button
        android:id="@+id/buttonret"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="131dp"
        android:text="Button" />
 
</RelativeLayout>

Screenshot_2013-08-13-12-21-23[1]

Sanfoundry Global Education & Learning Series – 100+ Java Android Tutorials.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all Tutorials, go to Java Android Tutorials.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.