How to load large images in Android and avoiding the out of memory error?

后端 未结 6 2041
梦谈多话
梦谈多话 2020-12-01 08:05

I\'m working on an app that uses large images (1390 × 870 : 150kb - 50kb). I\'m adding images as I tap a trigger/ImageView.

At a certain point I\'m getting an out of

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 08:50

    Here is how I'm adding each item to the FrameLayout that's the problem, the code keep adding and adding more images, and doesn't matter how well you resize or how much memory the device have, at certain point it WILL run out of memory. That's because every image you add it's keeping in memory.

    For this type of situation what the apps do is to use a ViewGroup that can recycle views. I don't know your layout, but usually is a ListView, GridView or a ViewPager, by recycling views you re-use the layout and can dispose re-load images as necessary.

    For the specific purpose of loading and resizing images I strongly advise use Picasso library as it is VERY well written, simple to use and stable.

提交回复
热议问题