2

In Android how to display audio files with thumbnails in to a List view?

enter image description here

karthik
  • 528
  • 4
  • 19
Rakhiii
  • 49
  • 5

1 Answers1

0

Here the code for get audio from storage-

public class SongsManager{
// SDCard Path

private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor;
String[] projection = { MediaStore.Audio.Media.ALBUM_ID,
        MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.ARTIST,
        MediaStore.Audio.Media.DURATION

};

@SuppressWarnings("deprecation")
public void getCursor(Activity context) {

    cursor = context.managedQuery(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,
            selection, null, null);

}

public ArrayList<HashMap<String, String>> getPlayList() {

    while (cursor.moveToNext()) {
        HashMap<String, String> song = new HashMap<String, String>();
        song.put("ID", cursor.getString(0));
        song.put("songTitle", cursor.getString(1));
        song.put("songPath", cursor.getString(2));
        song.put("Artist", cursor.getString(3));
        song.put("Duration", cursor.getString(4));
        songsList.add(song);
    }
    // return songs list array
    return songsList;
}

code for get audio thumb using audio id in ListAdapter getview method

Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");
        Uri uri = ContentUris.withAppendedId(sArtworkUri,
                Long.parseLong(songsList.get(position).get("ID")));
        songimage.setImageURI(uri);
Hitesh Gehlot
  • 1,307
  • 1
  • 15
  • 30
  • 2
    `managedQuery()` has been deprecated for **six years**. Please stop using it. Use a `ContentResolver` on a background thread, or use a `CursorLoader`. – CommonsWare May 01 '17 at 12:47
  • Seems like you should build the URI with Album ID, not song/track ID. See here for example: https://stackoverflow.com/q/3438809/997940 – Yoav Feuerstein Dec 06 '18 at 12:07