1

I am making a music player. so in album section, I try to load an image of an album by the Picasso library. images are loaded correctly but when I scroll then app lagging problem occur. I also try with glide but no improvement in performance.I think that this problem occurs due to image load from this link " content://media/external/audio/albumart ". is there any alternative method if not then how to improve scrolling performance..??

public class Song_Adapter extends 
RecyclerView.Adapter<Song_Adapter.MyViewHolder> implements 
FastScrollRecyclerView.SectionedAdapter {


ArrayList<Song_base> song_info;
Context context;
LayoutInflater inflater;
LayoutInflater inflater1;
Home home;
AlertDialog.Builder dialog;
AlertDialog dg;
TextView remove,play,search,send,set_ringtone,detail;
DatabaseHelperAdapter helper;


public Song_Adapter(ArrayList<Song_base> song_info, Context context) {
    this.song_info = song_info;
    this.context = context;
    inflater = LayoutInflater.from(context);
    inflater1 = LayoutInflater.from(context);

}

@Override
public Song_Adapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = inflater.inflate(R.layout.recycler_song_layout, parent, false);
    return new Song_Adapter.MyViewHolder(v);
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) 
 {

    final Song_base song = song_info.get(position);

    holder.song_name.setText(song.getSong_name());
    holder.artist.setText(song.getArtist());

    holder.duration.setText(Constants.calculatetime(Integer.parseInt(
    song.getDuration())));


     getimageart(song.getAlbumId(), song.getContext(), holder.bannerp 


}


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

public class MyViewHolder extends RecyclerView.ViewHolder {
    TextView song_name, artist,duration;
    CardView cardView;
    Button play;
    ImageView bannerp;
    RelativeLayout relativeLayout;

    public MyViewHolder(View itemView) {
        super(itemView);
        song_name = itemView.findViewById(R.id.song_name);
        artist = itemView.findViewById(R.id.artist);
        cardView = itemView.findViewById(R.id.cardview);
        bannerp = itemView.findViewById(R.id.small_banner);
        relativeLayout = itemView.findViewById(R.id.cardrelative);
        duration = itemView.findViewById(R.id.duration);

    }


}

public static void getimageart(Long albumId,Context context,ImageView image) {

    Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
    Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId);


    Bitmap bitmapimage = null;
    try {
        bitmapimage = MediaStore.Images.Media.getBitmap(
                context.getContentResolver(), albumArtUri);

    }
    catch (Exception exception) {
        exception.printStackTrace();

    }
    if(bitmapimage!=null)
    {
        Picasso.with(context)
                .load(Uri.parse(String.valueOf(albumArtUri)))
                .resize(200,200)
                .centerInside()
                .into(image);

    }


}


}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
D_lip
  • 67
  • 1
  • 7
  • 1
    Possible duplicate of [RecyclerView Scrolling Performance](https://stackoverflow.com/questions/27188536/recyclerview-scrolling-performance) – AskNilesh Mar 12 '18 at 12:59
  • Why are you using `MediaStore.Images.Media.getBitmap()`? Why don't only use the loader `Picasso`. – ADM Mar 12 '18 at 13:12
  • Thanks for giving idea.. after remove MediaStore.Images.Media.getBitmap Scrolling become very smooth. thanks again because i struck in this problem since many days – D_lip Mar 12 '18 at 13:24

1 Answers1

5

Use the recycler-view cache this will help you

recyclerView.setItemViewCacheSize(25);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

Use fit()

Picasso.with(context)
            .load(Uri.parse(String.valueOf(albumArtUri)))
            .resize(200,200)
            .fit()
            .centerCrop()
            .into(image);

The fit() actually resizes the image to fit into the imageView's bounds. This doesn't load the full image and smooth the scrolling.

Fazal Hussain
  • 1,129
  • 12
  • 28