View Pager with Universal Image Loader Out of Memory Error

后端 未结 7 1554
别跟我提以往
别跟我提以往 2021-02-02 01:58

I am not really sure if a ViewPager with Universal Image Loader can/should be used as an alternate for a gallery like interface since I have run into an Out of Memory error whil

相关标签:
7条回答
  • 2021-02-02 02:02

    I used kutothe's implementation from github issues page.

    0 讨论(0)
  • 2021-02-02 02:05

    I also used the same library and had same error. As solution, i created a sparseArray to keep photoView instances. And use it like this:

     private SparseArray<PhotoView> photoViewHolder;
    
     @Override
     protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
           ...
    
           photoViewHolder = new SparseArray<PhotoView>();
           ...
     }
    
    private class GalleryPagerAdapter extends PagerAdapter {
    
    @Override
    public View instantiateItem(ViewGroup container, int position) { 
    
            PhotoView photoView = new PhotoView(container.getContext());
    
            ImageHolder holder = new ImageHolder();
            holder.position = position;
            holder.loaded = false;
    
            photoView.setTag(holder);
            photoViewHolder.put(position, photoView);
    
                        // I used LazyList loading
            loader.DisplayImage(items.get(position), photoView);
    
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
    
            return photoView;
        }
    
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
        photoViewHolder.remove(position);
    }
    
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    
    }
    

    And to handle viewPager's listener:

       pager.setOnPageChangeListener(new OnPageChangeListener() { 
    
        @Override
        public void onPageScrollStateChanged(int position) { 
    
        } 
    
        @Override
        public void onPageScrolled(int position, float arg1, int arg2) { 
    
        } 
    
        @Override
        public void onPageSelected(int position) { 
            if(photoViewHolder.get(position) != null) {
                ImageHolder holder = (ImageHolder)photoViewHolder.get(position).getTag();
                // Do something...
            }
        } 
    });
    

    Hope this helps...

    0 讨论(0)
  • 2021-02-02 02:05

    I know it's late, but maybe my answer will save someone's time. After hours and hours of trying to solve this issue (with almost every answer found on stack overflow) I finally solved it with Fresco image library. It'a a lib written by Facebook and it's primary goal is to use memory in efficient way. It's really great and my Out Of Memory Error disappeared. I highly recommend using it.

    http://frescolib.org/

    0 讨论(0)
  • 2021-02-02 02:09

    Just posting this because this question is coming up on Google when searching for UIL and OOP. I had OOP problems no matter what configuration, what solved all my problems were the two classes RecyclingImageView and RecyclingBitmapDrawable from this sample project.

    0 讨论(0)
  • 2021-02-02 02:12

    It's probably not the best implementation to solve it, but it worked for me. Removing the ImageViews is not enough, so I decided to recycle bitmaps in 'destroyItem':

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        ImageView imageView = (ImageView) view.findViewById(R.id.image);
        if (imageView != null) {
            Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            bitmap.recycle();
            bitmap = null;
        }
        ((ViewPager) container).removeView(view);
        view = null;
    }
    

    This does not clean the last 3 active pages when you leave the activity, although I hope that GC takes care of them.

    0 讨论(0)
  • 2021-02-02 02:17

    Try to apply next suggestions:

    1. Use ImageScaleType.EXACTLY
    2. Enable caching on disc (in display options).
    3. Finally try to use .discCacheExtraOptions(maxImageWidthForDiscCache, maxImageHeightForDiscCache, CompressFormat.PNG, 0);
    0 讨论(0)
提交回复
热议问题