How do I get Album Thumbnails in Android?
I have a list of albums which I got using this:
开发者_如何学编程private List<Album> getAlbums() {
Cursor cur = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
List<Album> albums = new ArrayList<Album>();
if (cur.moveToFirst()) {
do {
int albumIdIndex = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
int albumIndex = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM);
int artistIndex = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST);
int albumId = cur.getInt(albumIdIndex);
String name = cur.getString(albumIndex);
String artist = cur.getString(artistIndex);
LogUtil.i(TAG, "albumid= " + albumId + ", album= " + name + ", artist=" + artist);
Album album = new Album(albumId, name, artist);
if (!albums.contains(album)) {
albums.add(album);
}
} while (cur.moveToNext());
}
return albums;
}
I use this list of albums in a ListActivity, but I also want to get the thumbnails of the Albums.
I know I can use the following code to get the actual artwork, but I need the thumbnails because I get Out Of Memory if I use the full images in a List Activity:
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
I saw this post: Android media thumbnails. Serious issues? But I do not have the imageId, only the albumid, so I don't think it helps.
Get each album art, create a new version of it using Bitmap.createScaledBitmap(), use that, recycle() the old bitmap to make memory go away, move to next image. Keep a HashMap or something to make sure you don't load the same album art twice or more (More than one track will have the same Album art id, cache it.
Should work well. (Works for me)
精彩评论