How do I get Album Thumbnails in Android?

孤街醉人 提交于 2019-11-26 19:55:25

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/3438809/how-do-i-get-album-thumbnails-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!