现在的位置: 首页 > 移动开发 > 正文

android 查看多张图片 出现 内存溢出

2019年06月09日 移动开发 ⁄ 共 579字 ⁄ 字号 评论关闭
转自http://www.cnblogs.com/lwbqqyumidi/p/3407626.html
如果在读取时加上图片的Config参数,可以跟有效减少加载的内存,从而跟有效阻止抛out of Memory异常。

  /**

     * 以最省内存的方式读取本地资源的图片

     * @param context

     * @param resId

     * @return

     */

    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);

        }

抱歉!评论已关闭.