Users Online

· Guests Online: 100

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

074 Java Android Program to Demonstrate Media Content Provider

Java Android Program to Demonstrate Media Content Provider

Here is source code of the Program to Demonstrate Media Content Provider in Andorid. 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

<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>

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.83 seconds
10,827,193 unique visits