Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
Recycler view with glide

I am facing following problems with given code:

  1. Images reload when I scroll back the recyclerview
  2. Wrong thumbnails are loaded(they keep changing when I scroll back and forth)
  3. UI hangs(I tried threading but problem is still there)

These problems are not present for video files(else part of outer if)

Inside videoadapter:

if(videoFiles.get(position).getType().equals(MediaStore.Files.FileColumns.MEDIA_TYPE_AUDIO+"")){
            if(coverpicture(videoFiles.get(position).getPath())!=null) {
                        Glide.with(mContext)
                                .load(coverpicture(videoFiles.get(position).getPath()))
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .placeholder(circularProgressDrawable)
                                .into(holder.thumbnail);
            }
            else {
                        Glide.with(mContext)
                                        .load(new File(videoFiles.get(position).getPath()))
                                        .placeholder(R.drawable.ic_baseline_music_note)
                                        .into(holder.thumbnail);
            }
} else {
            Glide.with(mContext)
                    .load(new File(videoFiles.get(position).getPath()))
                    .placeholder(circularProgressDrawable)
                    .into(holder.thumbnail);
}

This is how I call videoadapter:


videoAdapter = new VideoAdapter(getActivity(),videoFiles);

The function coverpicture:

private Bitmap coverpicture(String path) {
        final MediaMetadataRetriever[] mr = new MediaMetadataRetriever[1];
        final byte[][] byte1 = new byte[1][1];
        Thread ttt = new Thread(){
            @Override
            public void run() {
                super.run();
                mr[0] = new MediaMetadataRetriever();
                mr[0].setDataSource(path);
                byte1[0] = mr[0].getEmbeddedPicture();
                mr[0].release();

            }
        };
        ttt.start();
        while(true){
            if(!ttt.isAlive()){
                if(byte1[0] != null) {
                    return BitmapFactory.decodeByteArray(byte1[0], 0, byte1[0].length);
                }
                else {
                    return null;
                }
            }
        }
    }

EDit: wrong thumbnail problem is solved when I replace first glide statement with

(it would be useful if someone explained why)

Glide.with(mContext)
          .load(coverpicture(videoFiles.get(position).getPath()))
                                .diskCacheStrategy(DiskCacheStrategy.ALL)
                                .placeholder(circularProgressDrawable)
                                .into(new DrawableImageViewTarget(holder.thumbnail));
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

  1. In recyclerview adapter's onBindViewHolder , in a background thread, load the glide images first.Then show it using view.post
    new Thread(()->{
        Bitmap b =coverpicture(videoFiles.get(position).getPath());
        Glide.with(mContext).downloadOnly().load(b);
          holder.thumbnail.post(() -> {
           Glide.with(mContext).clear(holder.thumbnail);
           Glide.with(holder.thumbnail.getContext())
                          .load(new File(videoFiles.get(position).getPath()))
                          .placeholder(circularProgressDrawable)
                          .format(DecodeFormat.PREFER_RGB_565)
                          .into(new DrawableImageViewTarget(holder.thumbnail));

         }
   }).start();          

2 . Already mentioned in the post

3 . Changed coverpicture() to

   static Bitmap coverpicture(String path) {
        MediaMetadataRetriever mr;
        byte[] byte1 = new byte[1];
        mr = new MediaMetadataRetriever();
        mr.setDataSource(path);
        try {
            byte1 = mr.getEmbeddedPicture();
            mr.release();
        }catch (Exception e){
            e.printStackTrace();
        }
        if(byte1 != null) {
            return BitmapFactory.decodeByteArray(byte1, 0, byte1.length);
        }
        else {
            return null;
        }
    }

No need of threading inside coverpicture() as it is called from a background thread

ALSO, the following optimistaions in recyclerview will help

recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!