如何获得每首歌的照片?我知道它被称为专辑艺术。我从我的代码中获得了歌曲名称,艺术家,专辑,但没有专辑封面

时间:2017-02-02 06:41:24

标签: android

 String[] STAR = {"*"};

    Cursor cursor;
    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";


    cursor = getContentResolver().query(uri, STAR, selection, null, null);

    if (cursor != null) {

        if (cursor.moveToFirst()) {
            int i = 0;
            Drawable img;
            do {
                String songName = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));

                path[i] = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.DATA));

                String albumName = cursor.getString(cursor
                        .getColumnIndex(MediaStore.Audio.Media.ALBUM));
                albumId = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                song.add(songName);

//上面的代码完美无缺,但下一行会产生错误              String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));                     的System.out.println(路径);                     我++;                 }                 while(cursor.moveToNext());

1 个答案:

答案 0 :(得分:0)

您可以使用以下方法获取歌曲的专辑封面:

public static Bitmap getAlbumart(Context context, Long album_id){
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try{
          final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
          Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
          ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
          if (pfd != null){
              FileDescriptor fd = pfd.getFileDescriptor();
              bm = BitmapFactory.decodeFileDescriptor(fd, null, options);
              pfd = null;
              fd = null;
          }
      } catch(Error ee){}
      catch (Exception e) {}
      return bm;
    }