Home » Android » Android ViewPager inside RecycleView get RuntimeException: Canvas: trying to draw too large(207360000bytes) bitmap

Android ViewPager inside RecycleView get RuntimeException: Canvas: trying to draw too large(207360000bytes) bitmap

Posted by: admin November 1, 2017 Leave a comment

Questions:

I have a ViewPager with a complex view item inside a RecycleView. The RecycleView be putted in a Fragment. When I load my Fragment into Activity. I saw onCreateView method of Fragment is called. But I always get RuntimeException anvas: trying to draw too large(207360000bytes) bitmap as log :

E/AndroidRuntime: FATAL EXCEPTION: main
Process: myappname, PID: 8986
java.lang.RuntimeException: Canvas: trying to draw too large(207360000bytes) bitmap.
at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:260)
at android.graphics.Canvas.drawBitmap(Canvas.java:1415)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:545)
at android.widget.TextView.onDraw(TextView.java:5745)
at android.widget.CheckedTextView.onDraw(CheckedTextView.java:380)
at android.view.View.draw(View.java:17071)
at android.view.View.updateDisplayListIfDirty(View.java:16053)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.draw(View.java:17074)
at android.support.design.internal.ForegroundLinearLayout.draw(ForegroundLinearLayout.java:196)
at android.view.View.updateDisplayListIfDirty(View.java:16053)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.support.v7.widget.RecyclerView.drawChild(RecyclerView.java:4248)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.draw(View.java:17074)
at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3657)
at android.view.View.updateDisplayListIfDirty(View.java:16053)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.draw(View.java:17074)
at android.support.design.internal.ScrimInsetsFrameLayout.draw(ScrimInsetsFrameLayout.java:87)
at android.view.View.updateDisplayListIfDirty(View.java:16053)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.updateDisplayListIfDirty(View.java:16048)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1379)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.draw(View.java:17074)
at android.view.View.updateDisplayListIfDirty(View.java:16053)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.updateDisplayListIfDirty(View.java:16048)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.updateDisplayListIfDirty(View.java:16048)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.updateDisplayListIfDirty(View.java:16048)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.updateDisplayListIfDirty(View.java:16048)
at android.view.View.draw(View.java:16837)
at android.view.ViewGroup.drawChild(ViewGroup.java:3764)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3550)
at android.view.View.draw(View.java:17074)
at com.android.internal.policy.DecorView.draw(DecorView.java:751)
at android.view.View.updateDisplayListIfDirty(View.java:16053)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:656)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:662)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:770)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2796)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2604)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2211)

Here is my code :
1.My ViewPager Adapter :

    public class HighLightAdapter extends PagerAdapter {

    private final String TAG = "VT_HighLightAdapter";

    private List<HighLightModelReponse> dataModels;

    public HighLightAdapter(List<HighLightModelReponse> dataModels){
        Log.d(TAG,"HighLightAdapter contructor");
        this.dataModels = dataModels;
    }

    @Override
    public int getCount() {
        return dataModels.size();
    }


    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Log.d(TAG,"instantiateItem");
        View itemView = LayoutInflater.from(container.getContext()).inflate(R.layout.hightlights_items,container,false);
        RelativeLayout base_item = (RelativeLayout) itemView.findViewById(R.id.base_item);
        //header
        TextView header_catalog = (TextView) itemView.findViewById(R.id.tv_header_catalog);
        TextView header_details = (TextView) itemView.findViewById(R.id.tv_header_detail);
        //content image
        ImageView image_preview = (ImageView) base_item.findViewById(R.id.img_preview);
        ImageView image_delete = (ImageView) base_item.findViewById(R.id.img_delete_favorate);
        image_delete.setVisibility(View.INVISIBLE);
        //footer
        TextView footer_title = (TextView) base_item.findViewById(R.id.tv_footer_title);
        TextView footer_slogan = (TextView) base_item.findViewById(R.id.tv_footer_slogan);
        TextView footer_info = (TextView) base_item.findViewById(R.id.tv_footer_info);
        //rating
        RatingBar ratingBar = (RatingBar) base_item.findViewById(R.id.footer_rating);
        TextView numberRate = (TextView) base_item.findViewById(R.id.tv_number_rate);

        TextView numberComments = (TextView) base_item.findViewById(R.id.tv_number_comment);

        HighLightModelReponse highLightModelReponse = dataModels.get(position);
        String image_url = highLightModelReponse.getImage_url();
        if(image_url != null && !image_url.isEmpty()){
            Picasso.with(container.getContext())
                    .load(image_url)
                    .resize(image_preview.getWidth(),image_preview.getHeight())
                    .placeholder(R.drawable.ic_contact)
                    .error(R.drawable.ic_contact)
                    .into(image_preview, new Callback() {
                        @Override
                        public void onSuccess() {
                            Log.d(TAG,"Picasso onLoad success");
                        }

                        @Override
                        public void onError() {
                            Log.d(TAG,"Picasso onLoad Error");
                        }
                    });
           /* UiUtils.loadImageFromLink(container.getContext(),image_url,image_preview,image_preview.getWidth(),image_preview.getHeight());
            image_preview.setScaleType(ImageView.ScaleType.CENTER_CROP);*/
        }
        header_catalog.setText(highLightModelReponse.getCatalog());
        header_details.setText("Xem chi tiết");

        footer_title.setText(highLightModelReponse.getTitle());
        footer_slogan.setText(highLightModelReponse.getSlogan());
        footer_info.setText(highLightModelReponse.getMore_infor());

        ratingBar.setRating(highLightModelReponse.getAverRating());
        numberRate.setText(String.valueOf(highLightModelReponse.getNumberRating()));
        numberComments.setText(String.valueOf(highLightModelReponse.getNumberComments()));
        container.addView(itemView);
        return itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager)container).removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }
    }
  1. And here is my RecycleView Adaper :

    public class HighLightRecycleAdapter extends RecyclerView.Adapter {
    
    private final String TAG = "VT_HighLightRecycleAdapter";
    private List<List<HighLightModelReponse>> highLightData;
    
    private Context mContext;
    
    
    public HighLightRecycleAdapter(Context context,List<List<HighLightModelReponse>> datas){
        Log.d(TAG,"HighLightRecycleAdapter contructor");
        this.mContext = context;
        highLightData = datas;
    }
    
    
    
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.pager_item,parent,false);
        return new HighLightViewHolder(itemView);
    }
    
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        List<HighLightModelReponse> pagerDatas = highLightData.get(position);
        initializeViews(pagerDatas,holder,position);
    }
    
    @Override
    public int getItemCount() {
        return highLightData.size();
    }
    
    private void initializeViews(List<HighLightModelReponse> dataModel, final RecyclerView.ViewHolder holder, int position){
    
        Log.d(TAG,"initializeViews");
        HighLightAdapter adapter = new HighLightAdapter(dataModel);
        ((HighLightViewHolder)holder).viewPager.setAdapter(adapter);
        ((HighLightViewHolder)holder).viewPager.setClipToPadding(false);
        ((HighLightViewHolder)holder).viewPager.setPadding(40, 0, 40, 0);
        ((HighLightViewHolder)holder).indicator.setViewPager(((HighLightViewHolder)holder).viewPager);
        final float density = mContext.getResources().getDisplayMetrics().density;
        ((HighLightViewHolder)holder).indicator.setRadius(5 * density);
    }
    
    public static class HighLightViewHolder extends RecyclerView.ViewHolder{
    
        private final String TAG = "HighLightViewHolder";
    
        @BindView(R.id.view_pager)
        ViewPager viewPager;
        @BindView(R.id.indicator)
        CirclePageIndicator indicator;
    
        public HighLightViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            Log.d(TAG, "HighLightViewHolder contructor");
        }
    }
    }
    
  2. My Fragment

    public class HighLightFragment extends BaseFragment<HighLightPresenter> implements HighLightContract.View {
    
    private final String TAG = "VT_HighLightFragment";
    
    private List<List<HighLightModelReponse>> mHighLightData;
    
    @BindView(R.id.highlight_recycleview)
    RecyclerView mHighlightRecycleview;
    
    
    @Override
    protected int getLayoutId() {
        return R.layout.hightlights_activity;
    }
    
    @Override
    protected void initEventAndData() {
        Log.d(TAG,"initEventAndData");
        initDataHighLightModelTest();
    
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
    
        HighLightRecycleAdapter recycleAdapter = new HighLightRecycleAdapter(mContext,mHighLightData);
        mHighlightRecycleview.setAdapter(recycleAdapter);
        DividerItemDecoration mDividerItemDecoration = new DividerItemDecoration(mHighlightRecycleview.getContext(), mLayoutManager.getOrientation());
        mHighlightRecycleview.addItemDecoration(mDividerItemDecoration);
    }
    
    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
    
    
    private void initDataHighLightModelTest(){
        String catalog1 = "catalog1";
        String catalog2 = "catalog2";
        String catalog3 = "catalog3";
        String catalog4 = "catalog4";
    
        String title1 = "title1";
        String slogan1 = "slogan1";
        String detail1 = "detail1";
        float aver_rating1 = 4.5f;
        int rating_number1 = 7;
        int comment_number1 = 16;
        String image_cover1 = "https://khachsan.dulichvietnam.com.vn/uploads/image/tin-tuc-khach-san/khach-san-grand-plaza-ha-noi4.JPG";
    
        String title2 = "title2";
        String slogan2 = "slogan2";
        String detail2 = "";
        float aver_rating2 = 4.5f;
        int rating_number2 = 7;
        int comment_number2 = 16;
        String image_cover2 = "https://kenh14cdn.com/2016/3-1476470249125.jpg";
    
        String title3 = "title3";
        String slogan3 = "slogan3";
        String detail3 = "";
        float aver_rating3 = 4.5f;
        int rating_number3 = 7;
        int comment_number3 = 16;
        String image_cover3 = "http://vietnamtourism.gov.vn/images/2014/ManchinSaPa.jpg";
    
    
        HighLightModelReponse h1 = new HighLightModelReponse(catalog1,title1,slogan1,detail1,image_cover1,comment_number1,rating_number1,aver_rating1);
        /*HighLightModelReponse h2 = new HighLightModelReponse(catalog2,title2,slogan2,detail2,image_cover2,comment_number2,rating_number2,aver_rating2);
        HighLightModelReponse h3 = new HighLightModelReponse(catalog3,title3,slogan3,detail3,image_cover3,comment_number3,rating_number3,aver_rating3);*/
    
        List<HighLightModelReponse> pagerItem1 = Arrays.asList(new HighLightModelReponse[]{h1});
        /*List<HighLightModelReponse> pagerItem2 = Arrays.asList(new HighLightModelReponse[]{h1,h2,h3});
        List<HighLightModelReponse> pagerItem3 = Arrays.asList(new HighLightModelReponse[]{h1,h2,h3});*/
        mHighLightData = new ArrayList<>();
        mHighLightData.add(pagerItem1);
       /* mHighLightData.add(pagerItem2);
        mHighLightData.add(pagerItem3);*/
    }
    }
    
  3. And here is my activity code :

    public class MainActivity extends BaseActivity<MainPresenter> implements 
    MainContract.View, NavigationView.OnNavigationItemSelectedListener, 
    BottomNavigationView.OnNavigationItemSelectedListener {
    private final String TAG = "VT_MainActivity";
    
    private HighLightFragment mHighLightFragment;
    
    private int hideFragment = Constants.HIGH_LIGHT_FRAGMENT;
    private int showFragment = Constants.HIGH_LIGHT_FRAGMENT;
    
    @BindView(R.id.fragment_container)
    FrameLayout mFragmentContainter;
    
    @Override
    protected int getLayout() {
        return R.layout.activity_main;
    }
    
    @Override
    protected void initEventAndData() {
        Log.d(TAG,"initEventAndData");
        initFragment();
        /*showFragment = mPresenter.getCurrentFragment();
        hideFragment = Constants.HIGH_LIGHT_FRAGMENT;
        showHideFragment(getTargetFragment(showFragment), getTargetFragment(hideFragment));
        hideFragment = showFragment;*/
        mHighLightFragment = new HighLightFragment();
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.fragment_container,mHighLightFragment,"highlight");
        ft.commit();
    
    }
    
    
    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }
    
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int viewId = item.getItemId();
        switch (viewId) {
            case R.id.nav_login:
                break;
            case R.id.nav_notify:
                break;
            case R.id.nav_response:
                break;
            case R.id.nav_contact:
                break;
            case R.id.nav_rate:
                break;
            case R.id.nav_share:
                break;
            case R.id.nav_intro:
                break;
            case R.id.menu_hot:
                showFragment = Constants.HIGH_LIGHT_FRAGMENT;
                break;
            case R.id.menu_map:
                break;
            case R.id.menu_faviorate:
                break;
            case R.id.menu_event:
                break;
            case R.id.menu_register:
                break;
            default:
                break;
        }
        return true;
    }
    
    private void initFragment() {
        Log.d(TAG,"initFragment");
        mHighLightFragment = new HighLightFragment();
    }
    
    private Fragment getTargetFragment(int item) {
        switch (item) {
            case Constants.HIGH_LIGHT_FRAGMENT:
                Log.d(TAG,"getTargetFragment HIGH_LIGHT_FRAGMENT");
                return mHighLightFragment;
            case Constants.EVENT_FRAGMENT:
                return null;
            case Constants.MAP_FRAGMENT:
                return null;
            case Constants.FAVIORATA_FRAGMENT:
                return null;
            case Constants.REGISTER_FRAGMENT:
                return null;
            default:
                return null;
        }
    }
    
    private void showHideFragment(Fragment showFragment, Fragment hideFragment) {
        try {
            Log.d(TAG,"showHideFragment showFragment " + showFragment + "hideFragment " + hideFragment);
            FragmentManager fm = getSupportFragmentManager();
            fm.beginTransaction()
                    .remove(hideFragment)
                    .show(showFragment)
                    .commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    }
    

Somebody can help me !
Thank so much for your help

P/s : I can not post my layout and some base class of Fragment and Activity files because it make this post too large (more than 30000 characters). If you need any class or layout, please kindly free let me know.

Answers: