0

我知道这个问题被问了很多次,但我在下面的这段代码中有轻微的混淆

public class AudioAdapter extends RecyclerView.Adapter<AudioAdapter.AudioViewHolder> {
private List<MusicFiles> audioList;
private Context context;

public AudioAdapter(List<MusicFiles> audioList, Context context) {
    this.audioList = audioList;
    this.context = context;
}

@NonNull
@NotNull
@Override
public AudioViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(context).inflate(R.layout.custom_audio_item_layout, parent, false);
    return new AudioViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull @NotNull AudioAdapter.AudioViewHolder holder, int position) {
    holder.audioName.setText(audioList.get(position).getTitle());
    
    Log.d("dslafjk__", "imagePath :" + audioList.get(position).getPath());
    //Above Log return this path
    //imagePath :/storage/emulated/0/snaptube/download/SnapTube Audio/Angrej _ Full Songs Audio Jukebox _ Amrinder Gill(MP3_160K).mp3
    
    byte[] image = getAlbumImage(audioList.get(0).getPath());
    if (image != null) {
        Glide.with(context).asBitmap()
                .load(image)
                .into(holder.audioImage);
    } else {
        Glide.with(context)
                .load(R.drawable.music_image_placeholder)
                .into(holder.audioImage);
    }
    
}

@Override
public int getItemCount() {
    return audioList.size();
}

public class AudioViewHolder extends RecyclerView.ViewHolder {
    ImageView audioImage;
    TextView audioName;

    public AudioViewHolder(@NonNull @NotNull View itemView) {
        super(itemView);
        audioImage = itemView.findViewById(R.id.audioImage);
        audioName = itemView.findViewById(R.id.audioName);
    }
}

private byte[] getAlbumImage(String path) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(path);
    byte[] audioImage = retriever.getEmbeddedPicture();
    retriever.release();
    return audioImage;
}

}

retriever.setDataSource(path)正在生成错误我有 google 和 stack 很多次,但我找不到解决方案。我也检查了这些链接

如何使用 MediaStore 获取音频文件的缩略图

如何在android中显示音频文件的缩略图?

4

1 回答 1

0

试试这个方法可能对你有帮助

Bitmap image = ThumbnailUtils.createAudioThumbnail("your file name", MediaStore.Images.Thumbnails.MICRO_KIND);

或者试试这个

MediaMetadataRetriever mmr = new MediaMetadataRetriever();
    byte[] imgbyte;
    Bitmap bitmp;
    BitmapFactory.Options option =new BitmapFactory.Options();
    
   mmr.setDataSource(getApplicationContext(), uri);
    imgbyte = mmr.getEmbeddedPicture();
    

    if (imgbyte != null) 
        bitmp = BitmapFactory.decodeByteArray(imgbyte, 0,imgbyte.length, option);

或者试试这个

    Cursor cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
                    new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
                    MediaStore.Audio.Albums._ID+ "=?", 
                    new String[] {String.valueOf(albumId)}, 
                    null);
    
    if (cursor.moveToFirst()) {
        String albumart = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
        
Bitmap bm= BitmapFactory.decodeFile(albumart);
    }
于 2021-07-30T20:18:21.200 回答