android加载图片出现OOM

1.加载本地资源res/drawable出现OOM问题。
解决方法:

public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new  BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        //获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

这样在使用的时候调用aging方法,传递上下文和图片资源就可以获得bitmap,然后通过ImageView.setImageBitmap()方法,就可以了

2.glide加载网络大图出现OOM问题
这里用了取巧的方式:增大应用的内存空间(在application中添加这么一句)

 ...
     android:largeHeap="true"
     ...>
 

荆轲刺秦王

你可能感兴趣的:(崩溃处理,内存溢出,glide,内存溢出,oom,图片,glide)