Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listing album in android

I am developing a simple audio player in android. I want to list the album's in the device.

I tried this code

String where = new String();
where = MediaStore.Audio.Media.IS_MUSIC + "=1";
private Cursor managedCursor;
managedCursor = managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        new String[] {  
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.ALBUM,       
            MediaStore.Audio.Media.ALBUM_ID,    
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ARTIST_ID
        },
        where,                                  
        null,                                       
        MediaStore.Audio.Media.DEFAULT_SORT_ORDER   
    );

  ListAdapter adapter = new AlbumListAdapter(
        this,                                   
        R.layout.albumlist_item,                
        managedCursor,                          
        new String[] {                          
            MediaStore.Audio.Media.ALBUM,       
            MediaStore.Audio.Media.ARTIST           
        },
        new int[] {                             
            R.id.text_album, 
            R.id.text_artist 
        }
    );
    setListAdapter(adapter);

But this code is listing the all the song's in the device.

What is the structure of the Android Media store DB.

Any one please help.

like image 506
John Avatar asked Feb 03 '12 04:02

John


3 Answers

You should query the Albums like this

String[] projection = new String[] { Albums._ID, Albums.ALBUM, Albums.ARTIST, Albums.ALBUM_ART, Albums.NUMBER_OF_SONGS };
String selection = null;
String[] selectionArgs = null;
String sortOrder = Media.ALBUM + " ASC";
Cursor cursor = contentResolver.query(Albums.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, sortOrder);

http://developer.android.com/reference/android/provider/MediaStore.Audio.Albums.html

Artists, Playlists and Genres can all be queried in a similar way using the correct EXTERNAL_CONTENT_URI and the corresponding projection.

Hope that helps...

like image 126
JeffG Avatar answered Nov 16 '22 17:11

JeffG


use this simplified code to get list of albums

public ArrayList<AlbumModel> getListOfAlbums(Context context) {

            String where = null;

            final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
            final String _id = MediaStore.Audio.Albums._ID;
            final String album_name = MediaStore.Audio.Albums.ALBUM;
            final String artist = MediaStore.Audio.Albums.ARTIST;
            final String albumart = MediaStore.Audio.Albums.ALBUM_ART;
            final String tracks = MediaStore.Audio.Albums.NUMBER_OF_SONGS;

            final String[] columns = { _id, album_name, artist, albumart, tracks };
            Cursor cursor = context.getContentResolver().query(uri, columns, where,
                    null, null);

            ArrayList<AlbumModel> list = new ArrayList<AlbumModel>();

            // add playlsit to list

            if (cursor.moveToFirst()) {

                do {

                    AlbumModel albumData = new AlbumModel();

                    albumData
                            .setAlbumID(cursor.getLong(cursor.getColumnIndex(_id)));

                    albumData.setAlbumName(cursor.getString(cursor
                            .getColumnIndex(album_name)));

                    albumData.setALbumArtist(cursor.getString(cursor
                            .getColumnIndex(artist)));

                    albumData.setAlbumArt(cursor.getString(cursor
                            .getColumnIndex(albumart)));

                    albumData.setTracks(cursor.getString(cursor
                            .getColumnIndex(tracks)));

                    list.add(albumData);

                } while (cursor.moveToNext());
            }

            cursor.close();

            return list;
        }
like image 4
Hitesh Sahu Avatar answered Nov 16 '22 16:11

Hitesh Sahu


public class Album {

private long id;
private String albumName;
private String artistName;
private int nr_of_songs;
private Bitmap albumImg;

public Album(long id, String albumName, String artistName, Bitmap albumImg,  int nr_of_songs) {
    this.albumImg = albumImg;
    this.id = id;
    this.albumName = albumName;
    this.artistName = artistName;
    this.nr_of_songs = nr_of_songs;
}

public void setId(long id) {
    this.id = id;
}
public void setAlbumName(String albumName) {
    this.albumName = albumName;
}
public void setArtistName(String artistName) {
    this.artistName = artistName;
}
public void setAlbumImg(Bitmap albumImg) {
    this.albumImg = albumImg;
}
public void setNr_of_songs(int nr_of_songs) {
    this.nr_of_songs = nr_of_songs;
}

public long getID(){
    return id;
}
public String getAlbumName(){
    return albumName;
}
public String getArtistName() {
    return artistName;
}
public Bitmap getAlbumImg() {
    return albumImg;
}
public int getNr_of_songs() {
    return nr_of_songs;
}

}


 public void getAlbumsLists(){
    String where = null;

    final Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    final String _id = MediaStore.Audio.Albums._ID;
    final String album_name = MediaStore.Audio.Albums.ALBUM;
    final String artist = MediaStore.Audio.Albums.ARTIST;
    final String albumart = MediaStore.Audio.Albums.ALBUM_ART;
    final String tracks = MediaStore.Audio.Albums.NUMBER_OF_SONGS;

    final String[] columns = { _id, album_name, artist, albumart, tracks };
    Cursor cursor = context.getContentResolver().query(uri, columns, where, null, null);

    if(cursor!=null && cursor.moveToFirst()){

        do {

            long id = cursor.getLong(cursor.getColumnIndex(_id));
            String name = cursor.getString(cursor.getColumnIndex(album_name));
            String artist2 = cursor.getString(cursor.getColumnIndex(artist));
            String artPath = cursor.getString(cursor.getColumnIndex(albumart));
            Bitmap art = BitmapFactory.decodeFile(artPath);
            int nr =Integer.parseInt(cursor.getString(cursor.getColumnIndex(tracks)));

            albumList.add(new Album(id, name, artist2, art, nr));

        } while (cursor.moveToNext());
    }

    cursor.close();
}
like image 2
Nicu P Avatar answered Nov 16 '22 17:11

Nicu P