0

I am creating a file manager and I'd like to create thumbnails for videos/pictures on device. But when I scroll items too fast, the performance falls and sometimes my app just crashes because too many thumbnails are being created at one time

The main BIND part of my ViewHolder:

new Thread(new Runnable() {
                @Override
                public void run() {
                    final Bitmap[] btm = {null};
                    if(mimeType.startsWith("video/") || mimeType.startsWith("image/")){
                        if(mimeType.startsWith("video/")){
                            btm[0] = ThumbnailsHelper.createThumbForVideo(file.getAbsolutePath());
                        }else if(mimeType.startsWith("image/")){
                            btm[0] = ThumbnailsHelper.createThumbForPic(file.getAbsolutePath());
                        }
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mFileIcon.setImageBitmap(btm[0]);
                            }
                        });
                    }
                }
            }).start();

My RecyclerView settings:

    mRecyclerView.setItemViewCacheSize(20);
    mRecyclerView.setDrawingCacheEnabled(true);
    mRecyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

In apps like ZArchiver or similar I saw that thumbnails are shown only when user isn't scrolling RecyclerView items. So, how can I improve the situation and maybe anybody knows how to make it like in ZArchiver?

2 Answers2

0

The thing is when you scroll your recycler it create a lot of new threads which try to load your thumbnails.

You should:

  1. Use RxJava for thumbnail loading task to have the possibility to dispose this loading when view was scrolled and already not visible (recycled).

  2. When your view is recycled (onViewRecycled() method. See: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter) you should dispose this network request.

Another workaround is to create thumbnails as gif images for every video, store it on server and then show it with Glide (see: Show GIF file with Glide (image loading and caching library) and https://biteable.com/blog/tips/make-gif-video/)

Peter Staranchuk
  • 1,343
  • 3
  • 14
  • 29
0

I can't tell about the efficiency or [the right] method of doing what you are doing, but since you asked you wanted to know how to make thumbnail when user is not scrolling, I will tell you just that!

The RecyclerView can be attached with a scroll listener, and you can do what you want inside its methods. Let me show you :

myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);

        if (newState == RecyclerView.SCROLL_STATE_IDLE) {
            //Recyclerview isnt' scrolling!
            //do the work 
            makeThumbnail();
        }
        if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
            //Recyclerview scrolling is slowing down and about to stop

            makeThumbnail(); //optional
        }

        if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
            //RecyclerView scrolling is started
            stopPendingThumbnailMakingProcess(); //in case that's what you want to do!
        }
    }
});

Hope it helps ! Cheers!

bijaykumarpun
  • 677
  • 4
  • 9