3

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));
ADP
  • 107
  • 10
  • Is the function coverpicture to get frame from video file? – S T Nov 18 '20 at 05:38
  • it is to get thumbnail from audio files – ADP Nov 18 '20 at 05:39
  • for video files I don't use that function.maybe that is the reason why it loads faster and smoother... – ADP Nov 18 '20 at 05:40
  • how about this? https://stackoverflow.com/a/34683610/10778405 – S T Nov 18 '20 at 05:45
  • ig it is like just viewing a video through its thumbnails...i just want one thumbnail from one audio....but as there are many media in my device,so it shows wrong thumbnail(have no idea why) – ADP Nov 18 '20 at 05:50

1 Answers1

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);
ADP
  • 107
  • 10