Android从MediaStore获取专辑封面艺术

时间:2018-06-18 09:37:11

标签: android audio mediastore albumart

我从Strings获得了我需要的所有Audio但我似乎无法获得专辑封面图片。我试过用MediaStore.Audio.Albums.ALBUM_ART来获取它,但它没有给我任何东西。我究竟做错了什么?

public class AudioManager {

public static ArrayList<Audio> LoadAudioForSongList(Activity activity) {

    ArrayList<Audio> audioList = new ArrayList<>();

    ContentResolver contentResolver = activity.getContentResolver();

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
    String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

    Cursor cursor = contentResolver.query(uri, null, selection, null, sortOrder);

    if (cursor != null && cursor.getCount() > 0) {

        while (cursor.moveToNext()) {

            String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
            String duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

            //Album art
            String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

            // Save to audioList
            audioList.add(new Audio(data, title, album, artist, duration, url));
        }
    }

    if (cursor != null) {
        cursor.close();
    }
    return audioList;
}
}

0 个答案:

没有答案