Outofmemeoryerror (viewpager + imageviews)

后端 未结 6 1004
悲哀的现实
悲哀的现实 2021-01-30 12:18

i am showing 150+ images in viewpager, when page size crossed 70 app is crashing , all images are loading from network , and i have fallowed [link]: Strange out of memory issue

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 12:32

    Sheetal,

    I don't have my code in front of me at the minute but it should be something similar to the following:

    public class MyFragment extends Fragment {
      private Resources resources;  // I load my images from different resources installed on the device
      private int id;
    
      public MyFragment() {
        setRetainInstance(true); // this will prevent the app from crashing on screen rotation
      }
    
      public MyFragment(Resources resources, int id) {
        this();  // this makes sure that setRetainInstance(true) is always called
        this.resources = resources;
        this.id = id;
      }
    
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ImageView imageView = (ImageView)inflater.infale(R.layout.fragmentview, container, false);
        imageView.setImageBitmap(BitmapFactory.decodeResource(resources, id));
    
        return imageView;
      }
    }
    

    This is pretty much off the top of my head, so it might not be exact, but it is pretty close.

    If you need any more help just shout, don't forget to mark this answer as having helped you:).

提交回复
热议问题