3

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.

Community
  • 1
  • 1
timothyjc
  • 2,188
  • 3
  • 29
  • 54

1 Answers1

4

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)

Moncader
  • 3,388
  • 3
  • 22
  • 28
  • This was the approach I took but I think there is a URI for album art too: content://media/external/audio/albumart_thumb. I am actually stuck on writing Bitmaps to the URI content://media/external/audio/albumart at the moment. I am getting FileNotFoundExceptions when I use this code: Uri artworkUri = Uri.parse("content://media/external/audio/albumart"); Uri uri = ContentUris.withAppendedId(artworkUri, album.getId()); OutputStream outStream = getContentResolver().openOutputStream(uri); bmp.compress(Bitmap.CompressFormat.JPEG, 50, outStream); – timothyjc Aug 10 '10 at 13:21
  • 2
    Oh - I figured out the writing albumart part from here: http://www.netmite.com/android/mydroid/packages/apps/Music/src/com/android/music/MusicUtils.java – timothyjc Aug 10 '10 at 15:06
  • Hey i am also having trouble to change URI to bitmap..can you tell the code please? – Ankit Srivastava Nov 20 '13 at 18:26