【问题标题】:Retrieve images for songs android检索歌曲android的图像
【发布时间】:2016-10-21 16:29:39
【问题描述】:

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

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());
    }
}

我正在尝试将它放在列表视图中,所以这里是适配器:-

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;             
    }

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

【问题讨论】:

  • 一定是有原因的,如果CD封面叫“AlbumArt”而不是“SongArt”……你想让每首歌都有不同的画面吗?!然后让艺术家付钱给他们的绘图员,为每首歌制作一张图片。但我猜你会为 CD 支付两倍的费用。
  • 是的,我知道这是专辑,但问题是在每个列表项中只有一个位图
  • 我很高兴专辑中的所有歌曲都拥有相同的专辑图片。在这里,我认为问题不大。

标签: android


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多