如何在 Android 中获取相册缩略图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3438809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get Album Thumbnails in Android?
提问by timothyjc
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.
我在 ListActivity 中使用此专辑列表,但我也想获取专辑的缩略图。
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.
我看到了这篇文章: Android 媒体缩略图。严重的问题?但我没有 imageId,只有专辑,所以我认为它没有帮助。
回答by Moncader
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.
获取每个专辑封面,使用 Bitmap.createScaledBitmap() 创建它的新版本,使用它,recycle() 旧位图使内存消失,移动到下一张图像。保留一个 HashMap 或其他东西,以确保您不会两次或更多次加载相同的专辑封面(多个曲目将具有相同的专辑封面 ID,缓存它。
Should work well. (Works for me)
应该工作得很好。(对我有用)