RecyclerView利用Glide加载大量图片into(Target)导致OOM异常

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_38079174/article/details/70998973

RecyclerView利用Glide加载大量图片into(Target)导致OOM异常

    这是我的第一篇博客,此前很早就有写博客记录自己学习路程的想法,但都由于个人问题而没能实现,今天刚好趁着遇到的这个问题网上都没有很好的解决方案,开启自己的博客之旅,之后还会陆续更新,把以前的学习经历也补回来。好了,进入今天的正题。
    学过android的人应该都知道Glide是一个无比强大的图片加载库,它内部已经提供了很好的缓存机制供我们选择,我们只需一个参数调用即可(DiskCacheStrategy()),而不必像Universal-Image-Loader类一样自己实现。正是因为它太好用了,导致我们很容易忽略一些重要的细节。Android的bitmap对象是最容易导致OOM的元凶之一,如果我们加载大量的bitmap对象,我们就不得不预防OOM。
    我的原意是想将图片加载进RecyclerView里以StaggeredGridLayoutManager模式2列显示,实现瀑布流效果,

mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

但是因为图片都是网络中获取到的,不知道其具体大小,因此一开始采用target来实现,具体如下:

Glide.with(itemView.getContext())
     .load(url)
     .asBitmap()
     .placeholder(R.drawable.error_pic)
     .diskCacheStrategy(DiskCacheStrategy.ALL)
     .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
        @Override
        public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
            //原始图片宽高
            int imageWidth = resource.getWidth();
            int imageHeight = resource.getHeight();
            //按比例收缩图片
            float ratio=(float) ((imageWidth*1.0)/(width*1.0));
            int height=(int) (imageHeight*1.0/ratio);
            ViewGroup.LayoutParams params = ivImage.getLayoutParams();
            params.width=width;
            params.height=height;
            ivImage.setImageBitmap(resource);
        }
    });

而在此之前我通过这种方法也确实能够加载,但加载的都是一些几十KB的小图片,也没有遇到OOM的问题。但是在我现在所做得这个项目中,因为需要加载的图片的源图片是MB级别的,所以不能像之前一样将其加载到bitmap里。
     后来想起以前没用Glide之前有种方法可以不需要加载图片进bitmap也能获取到图片的宽高,便想着能否将其结合着使用。以下为在不加载图片的情况下获取图片宽高的方法:

//在不加载图片情况下获取图片大小
public static int[] getImageWidthHeight(String path)
{
    BitmapFactory.Options options = new BitmapFactory.Options();
    /**
     * 最关键在此,把options.inJustDecodeBounds = true;
     * 这里再decodeFile(),返回的bitmap为空,但此时调用options.outHeight时,已经包含了图片的高了
     */
    options.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeFile(path, options); // 此时返回的bitmap为null
    /**
     *options.outHeight为原始图片的高
     */
    return new int[]{options.outWidth,options.outHeight};
}

这样我就获取到原图片的宽高了,那么接下来我就不需要再使用target对象来获取图片,而是直接重新设置图片宽高即可,但在此之前还的将图片等比例缩放:

 //获取屏幕宽度
DisplayMetrics outMetrics = new DisplayMetrics();
WindowManager manager=getWindowManager();
manager.getDefaultDisplay().getMetrics(outMetrics);
width = outMetrics.widthPixels/2;

//按宽度等比例缩放,不然会OOM
int[] width_height= FileHelper.getImageWidthHeight(NetUrl.dir+"/"+data);
float ratio=(float) ((width_height[0]*1.0)/(width*1.0));
int height=(int) (width_height[1]*1.0/ratio);

最后直接调用glide重新设置大小即可:

Glide.with(itemView.getContext())
     .load(url)
     .asBitmap()
     .placeholder(R.drawable.error_pic)
     .diskCacheStrategy(DiskCacheStrategy.RESULT)
     .override(width,height)

如此,问题就解决了。
    好了,最后再次说明一下,本文为我的第一篇博客,如有什么错误,希望大家不吝赐教,我也会不断改进的,谢谢大家了!O(∩_∩)O嘻嘻~

猜你喜欢

转载自blog.csdn.net/m0_38079174/article/details/70998973