Out Of Memory Error When loading more images in Glide

前端 未结 11 912
我寻月下人不归
我寻月下人不归 2020-12-31 06:32

Edited:

  • In my application, I am loading more than 300 images in home page. I used glide to load images. I\'m getting Out of
相关标签:
11条回答
  • 2020-12-31 06:33

    The reason is while scrolling, glide keep doing image process even related views removes from list. Add this code in your listview's onScrollStateChanged.

    if (view.getContext() != null) {
            switch (scrollState) {
                case SCROLL_STATE_IDLE:
                    Glide.with(view.getContext()).resumeRequests();
                    break;
                case SCROLL_STATE_TOUCH_SCROLL:
                case SCROLL_STATE_FLING:
                    Glide.with(view.getContext()).pauseRequests();
                    break;
            }
        }
    
    0 讨论(0)
  • 2020-12-31 06:36

    Using Glide doesn't guarantee no Out of Memory errors, you need to use several small steps to reduce the probability to not get OOM's.

    Step 1: Understand the caching mechanism in Glide

    Step 2: I prefer to load thumbnails into recyclerview

    Glide  
        .with( context )
        .load( UsageExampleGifAndVideos.gifUrl )
        .thumbnail( 0.1f )
        .into( imageView2 );
    

    Remember to always request small size image if bigger or HD images are not required.

    0 讨论(0)
  • 2020-12-31 06:37

    Use recyclerView instead of ListView. It reusable single item for rendering items. I am using glide with recyclerView where i am loading wallpapers with 100+ items.

    In ListView every time you are creating view, if you have 100+ view and it will create 100+ views where as in recyclerview it creates how many visible items are there in screen +2.

    0 讨论(0)
  • 2020-12-31 06:44

    I faced the similar problem . I am sharing the way I solved it . Create a folder named drawable-nodpi put your golive_load_image and golive_cancel_im‌​age file into that folder , and remove those two image file from other place like drawable-ldpi,drawable-hdpi etc (if you have there ) . And add skipMemoryCache( true )

         Glide.with(context).load(rowItem.getPosteduserpostimage())
                                .skipMemoryCache( true )
                                .placeholder(R.drawable.golive_load_image).error(R.drawable.golive_cancel_image)
                                .override(600, 200)
                                .into(holder.ivPostedImage);
    
    0 讨论(0)
  • 2020-12-31 06:49
    1. Make sure the ImageView has match_parent or fixed dp as dimensions wrap_content makes Glide load full resolution Bitmaps.
    2. .placeholder() shows an image instead of empty space while loading large bitmap
    3. .thumbnail(float) loads a downsampled version fast while the bigger image is loading in the background
    4. Also look around the Glide issues, maybe you find something helpful.
    0 讨论(0)
  • 2020-12-31 06:53

    I faced the same issue and solved it by adding android:largeHeap="true" in the application tag of my manifest file like below.

    <manifest>
       ...
       <application
        .....
        android:largeHeap="true"
        ....
       >
       ....
       </application>
    </manifest>
    

    NB: this should be your last option, as using largeHeap:true is not recommended for solving simple OOM problems.

    0 讨论(0)
提交回复
热议问题