android 中歌曲的专辑封面显示不正确

Album art for song is not showing correct in android

这是我从本地存储中获取歌曲的光标:

cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.AudioColumns.DURATION+">0", null, sortOrder);

我正在使用另一个光标显示相册,如下所示,因为我无法使用相同的光标执行此操作:

ContentResolver musicResolve = getContentResolver();
Uri smusicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;    
Cursor musicCursorTogetAlbum =musicResolve.query(smusicUri,null, null, null, null);

我正在显示这样的相册,但显示不正确:

    musicCursorTogetAlbum.moveToFirst();
    musicCursorTogetAlbum.move(cursorPosition);

    int x = musicCursorTogetAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
    int id_albumCursor = musicCursorTogetAlbum.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);

    String thisArt = musicCursorTogetAlbum.getString(x);

    Bitmap bm = BitmapFactory.decodeFile(thisArt);
    Bitmap bm_temp = BitmapFactory.decodeFile(thisArt);

    Drawable dr = new BitmapDrawable(getResources(), bm);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {  
         iv_album_art.setImageBitmap(bm);
    }

cursorPosition 是 int 类型变量,它给出了 cursor 光标的位置,我用它来从本地存储中获取歌曲。

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

`

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

`