简体   繁体   中英

Android App Development(Music Player)

I am working on a project of media player where i have fetched all the songs from the sd card as shown in below picture . See my app layout image here.. enter image description here

but now i want to add their corresponding song album image at the image view on the list view where the green android image is .... Like this I want to create it........ See here how i want to create list with pictures enter image description here

So please tell me how to get the album art of the each song and set them to the listview infront of their names.........

My Codes to fetch the songs are as follows..........

public class SongList extends Activity{
    Cursor c;
    int index;
    int count;
    String audioFilePath;
    MediaPlayer mp;

    ArrayList songs;

    public  void onCreate(Bundle SIS){
        super.onCreate(SIS);
        setContentView(R.layout.song_list);

        String orderBy= MediaStore.Audio.Media.TITLE;
        String cols[]=     {MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME};
        c=this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,cols,null,null,orderBy);

    songs=new ArrayList();
    if(c!=null){
        while(c.moveToNext()){
            songs.add(c.getString(1).toString());
        }
    }
    ArrayAdapter adapter = new     ArrayAdapter(this,R.layout.custom_list_layout,R.id.listtext,songs);
    final ListView lv =(ListView)findViewById(R.id.songlist);
    lv.setAdapter(adapter);

Few years ago, I developed a music app too and this is the function where I retrieve the album list.
NB : its been almost 4 years now and I did not change my code so it may won't fit with new Android versions

private void        retrieveAlbums(){
    try {
        String      selection = MediaStore.Audio.Albums._ID + " != 0";
        String[]    projection = {
                "DISTINCT " + MediaStore.Audio.Albums._ID,
                MediaStore.Audio.Albums.ALBUM_KEY,
                MediaStore.Audio.Albums.ALBUM, 
                MediaStore.Audio.Albums.ARTIST,
                MediaStore.Audio.Albums.NUMBER_OF_SONGS
        };

        Cursor      cursor = context.getContentResolver().query(
                MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                projection, selection, null, null);

        Album       tmp = null;

        if (cursor.moveToFirst()) {
            do {

                tmp = new Album(cursor.getInt(0),
                        cursor.getString(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getInt(4));

                Uri sArtworkUri = Uri
                        .parse("content://media/external/audio/albumart");
                Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, tmp.getId());

                Bitmap bitmap = null;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(
                            context.getContentResolver(), albumArtUri);

                } catch (FileNotFoundException exception) {
                    exception.printStackTrace();
                    bitmap = BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.ic_launcher);
                } catch (IOException e) {
                    e.printStackTrace();
                    bitmap = BitmapFactory.decodeResource(context.getResources(),
                            R.drawable.ic_launcher);
                    e.printStackTrace();
                }
                tmp.setArt(bitmap);
                albumList.add(tmp);
            } while (cursor.moveToNext());
        }
        cursor.close();
    }  catch (SQLException e) {
        Log.e("Exception on query albums", e.toString());
    }
}

And the Album class

public class Album {
private int     id;
private String  key;
private String  name;
private String  artist;
private int     nbSong;
private Bitmap  art;
private Bitmap  banner;

public Album(int id, String key, String name, String artist, int nbSong) {
    super();
    this.id = id;
    this.key = key;
    this.name = name;
    this.artist = artist;
    this.nbSong = nbSong;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getNbSong() {
    return nbSong;
}
public void setNbSong(int nbSong) {
    this.nbSong = nbSong;
}
public String getArtist() {
    return artist;
}
public void setArtist(String artist) {
    this.artist = artist;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public String getKey() {
    return key;
}
public void setKey(String key) {
    this.key = key;
}
public Bitmap getArt() {
    return art;
}
public void setArt(Bitmap art) {
    this.art = art;
}
public Bitmap getBanner() {
    return banner;
}
public void setBanner(Bitmap banner) {
    this.banner = banner;
}
}

Hope it could help you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM