好的,所以我是Android的新手和网站本身。 我正在尝试为我的音乐播放器检索专辑封面。
public void getSonglist(){
Songs song;
ContentResolver songResolver = getContentResolver();
Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri artUri;
Cursor songCursor = songResolver.query(songUri, null, null, null, null);
final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
if(songCursor.moveToFirst()){
do{ song = new Songs("","",0);
albumID = songCursor.getLong(songCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
song.setTitle(songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
song.setArtist(songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
song.set_Id(songCursor.getLong(songCursor.getColumnIndex(MediaStore.Audio.Media._ID)));
artUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumID);
AlbumArt = null;
try {
AlbumArt = (MediaStore.Images.Media.getBitmap(getContentResolver(), artUri));
} catch (Exception exception) {
// log error
}
song.setAlbumArt(AlbumArt);
songlist.add(song);
}while(songCursor.moveToNext());
}
}
这里的问题是虽然代码没有错误,但我的设备上有大量的歌曲(大约900),并且在尝试加载专辑封面时应用程序内存不足。 (E / art:Throwing OutOfMemoryError“无法分配1000012字节分配,其中331252个空闲字节和323KB直到OOM”)
有人能告诉我另一种更好的方式获得专辑封面? (如果可能的话,请用代码支持你的回答。)