检索歌曲android的图像

时间:2016-10-21 08:23:12

标签: android

我想检索列表中所有歌曲的图像,但我只获得一张所有歌曲的图像。请告诉我我错在哪里。

ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor cursor = musicResolver.query(musicUri, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        int titleColumn = cursor
                .getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = cursor
                .getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
        Long albumId = cursor.getLong(cursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
        int artistColumn = cursor
                .getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
        do {

            Songs song = new Songs();
            song.id = cursor.getLong(idColumn);
            song.title = cursor.getString(titleColumn);
            song.artist = cursor.getString(artistColumn);
            Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri,
                    albumId);

            song.imagePath = albumArtUri;

            songList.add(song);

        } while (cursor.moveToNext());
    }
}

我想把它放在listview中,所以这里是适配器: -

public View getView(int arg0, View arg1, ViewGroup arg2) {
         LinearLayout songLay = (LinearLayout)songInf.inflate
                  (R.layout.cust_list_song, arg2, false);
              //get title and artist views
              TextView songView = (TextView)songLay.findViewById(R.id.song_title);
              TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
              ImageView imageSong = (ImageView) songLay.findViewById(R.id.song_cover);
              Songs currSong = songs.get(arg0);
              songView.setText(currSong.title);
              artistView.setText(currSong.artist);

              Bitmap bitmap = null;
              try {
                  bitmap = MediaStore.Images.Media.getBitmap(
                          c.getContentResolver(), currSong.imagePath);
                  bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true);
                  imageSong.setImageBitmap(bitmap);
              } catch (FileNotFoundException exception) {
                  exception.printStackTrace();
              } catch (IOException e) {

                  e.printStackTrace();
              }

              songLay.setTag(arg0);
              return songLay;             
    }

我是初学者,所以需要帮助。

1 个答案:

答案 0 :(得分:0)

正如我在您的代码中看到的那样,这是因为您使用相册的图像而不是歌曲图像的明显结果

Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri,
                albumId); 
song.imagePath = albumArtUri;// You set the album art uri as song's image path