解决一个因Bitmap引起的OOM问题

发布时间:2020-06-14 10:18:04 作者:chainli
来源:网络 阅读:1156

BitmapFactory.decodeStream引起了OutOfMemory. 在decodeStream之前,通过设置options.inJustDecodeBounds = true;让Bitmap实现虚加载。

设置options.inJustDecodeBounds = true;让Bitmap实现虚加载。 使用options.outWidth和options.outHeight获取图片宽和高。 再使用ImageLoader.getInstance().displayImage加载图片。

public class PhotoViewFragment extends Fragment {

    private PhotoView mGestureImageView;
    private Bitmap mBitmap;

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        View inflateView = inflater.inflate(R.layout.fragment_photo_view_show, container, false);
        mGestureImageView = (PhotoView) inflateView.findViewById(R.id.p_w_picpath_turn_show_gestureImageView);
        getBundleData();
        try {
            /*减少内存占用*/
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inPreferredConfig = Bitmap.Config.RGB_565;
            opt.inPurgeable = true;
            opt.inInputShareable = true;
            opt.inJustDecodeBounds = true;//虚加载

            FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory() + File.separator + "abc.png");
            mBitmap = BitmapFactory.decodeStream(fis, null, opt);

            int width = opt.outWidth;
            int height = opt.outHeight;

            if (height / width >= 1.6) {//判断横竖图
                mGestureImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            } else {
                mGestureImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            }

            DisplayImageOptions op = new DisplayImageOptions.Builder()
                    .cacheInMemory(true) // default
                    .build();
            Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() + File.separator + "abc.png"));
            ImageLoader.getInstance().displayImage(uri.toString(), mGestureImageView, op);
        } catch (Exception e) {
            Log.e("Fragment", "onCreateView p_w_picpath-view", e);
        }
        return inflateView;
    }

    /**
     * 重置GestureImageView为原大小
     */
    public void resetGestureImageView() {
        mGestureImageView.reset();
    }

    /**
     * 获得Bundle
     */
    private void getBundleData() {
        Bundle bundle = getArguments();
    }

    @Override
    public void onPause() {
        super.onPause();
        mGestureImageView.reset();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mBitmap != null && !mBitmap.isRecycled()) {
            mBitmap.recycle();
            mBitmap = null;
        }
    }


}


推荐阅读:
  1. 解决TensorFlow GPU版出现OOM错误的问题
  2. 一次因HashSet引起的并发问题详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

bitmap outofmemory bit

上一篇:28面向对象3_继承_linkedlist

下一篇:Oracle中分区表中表空间属性

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》