0

I know this question is asked many times but I have minor confusion in this code below

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) is generating the error I have google and stack many times but I can't found the solution. I have also check these links

How to get thumbnails of audio files by using MediaStore

How to display a thumbnail for audio files in android?

Sajid Ali
  • 61
  • 6

1 Answers1

1

Try this way may help you

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

Or try this

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

Or try this

    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);
    }
gpuser
  • 1,143
  • 1
  • 9
  • 6
  • 'createAudioThumbnail(java.lang.String, int)' is deprecated as of API 29: Android 10.0 (Q) – Sajid Ali Jul 31 '21 at 05:19
  • Please try this Added in API level 29 public static Bitmap createAudioThumbnail (File file, Size size, CancellationSignal signal). https://developer.android.com/reference/android/media/ThumbnailUtils – gpuser Jul 31 '21 at 10:10
  • Also please try with mediaExtractor or check third option I have updated in answer through albumArt above – gpuser Jul 31 '21 at 10:30
  • @gpuser added and deprecated in Api level 29 check the doc – Adarsh Singh May 24 '22 at 19:03