我在Android Studio上制作了一个简单的MP3播放器,我知道如何获得艺术家姓名和歌曲标题,但我不知道如何获取专辑封面图像(在ImageView中设置) 这是我用来获取并在ListView上显示带有艺术家和标题
的曲目的代码public void getSongList() {
ContentResolver trackResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor trackCursor = trackResolver.query(musicUri, null, null, null, null);
if(trackCursor!=null && trackCursor.moveToFirst()){
//get columns
int titleColumn = trackCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int artistColumn = trackCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
//add songs to list
do {
String thisTitle = trackCursor.getString(titleColumn);
String thisArtist = trackCursor.getString(artistColumn);
songList.add(new Song(thisId, thisTitle, thisArtist));
}
while (trackCursor.moveToNext());
}
}
答案 0 :(得分:1)
用它来获取专辑封面的uri
public static Uri getArtUriFromMusicFile(Context context, File file) {
final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
final String[] cursor_cols = { MediaStore.Audio.Media.ALBUM_ID };
final String where = MediaStore.Audio.Media.IS_MUSIC + "=1 AND " + MediaStore.Audio.Media.DATA + " = '"
+ file.getAbsolutePath() + "'";
final Cursor cursor = context.getApplicationContext().getContentResolver().query(uri, cursor_cols, where, null, null);
/*
* If the cusor count is greater than 0 then parse the data and get the art id.
*/
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);
cursor.close();
return albumArtUri;
}
return Uri.EMPTY;
}