Skip to content

Instantly share code, notes, and snippets.

@novoda
Created April 21, 2010 23:01
Show Gist options
  • Save novoda/374533 to your computer and use it in GitHub Desktop.
Save novoda/374533 to your computer and use it in GitHub Desktop.
Android: List all music files
//Retrieve a list of Music files currently listed in the Media store DB via URI.
//Some audio may be explicitly marked as not being music
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION
};
cursor = this.managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()){
songs.add(cursor.getString(0) + "||" + cursor.getString(1) + "||" + cursor.getString(2) + "||" + cursor.getString(3) + "||" + cursor.getString(4) + "||" + cursor.getString(5));
}
@chiragdhunna
Copy link

How to use it?

  1. You have to add a listview in xml
  2. Then name that listview and add findviewbyid method to it
  3. Then use a adaptor whichever you can use
  4. For eg : ArrayAdapter ad = new ArrayAdapter(this,android.R.id.layout.simple_list_view_1,songs);
    listview.setadapter(ad);

then it will list those .mp3 files in your activity!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment