Monday, February 27, 2012

List audio media in MediaStore.Audio.Media

This exercise list audio in MediaStore.Audio.Media on a ListView of ListActivity.
List audio media in MediaStore.Audio.Media
package com.exercise.AndroidListMedia;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class AndroidListMediaActivity extends ListActivity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      String[] from = {
        MediaStore.MediaColumns.TITLE};
      int[] to = {
        android.R.id.text1};
    
      Cursor cursor = managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        null,
        null,
        null,
        MediaStore.Audio.Media.TITLE);
    
      ListAdapter adapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_list_item_1, cursor, from, to);
      setListAdapter(adapter);
  }
}


Download the files.

Next:
- Get info of a specified item in MediaStore.Audio.Media

Remark:
Please notice that both managedQuery(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) and SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) are deprecated. See how to use CursorLoader here.

1 comment:

  1. Sir this tutorial works great, i need their thumbnail also, can you please tell me how i can add their thumbnail before the songs title.
    I will be very thankful to you.

    ReplyDelete