Android: Images from XML layout to Bitmaps in Code to solve OOM error

痴心易碎 提交于 2019-12-23 04:28:29

问题


I am getting the infamous java.lang.OutOfMemoryError: bitmap size exceeds VM budget error. I have several XML layouts and a lot of the elements have images (button backgrounds, Linear layout backgrounds, etc.) After going through the questions posted, like Strange out of memory issue while loading an image to a Bitmap object and external allocation too large for this process and OutofMemoryError: bitmap size exceeds VM budget (Android) and Android: Error loading images: OutOfMemoryError: bitmap size exceeds VM budget I know I should use the BitMapFactory methods. But all my images are defined in my XML for example,

    <ImageView
        android:layout_width="80dp"
        android:layout_height="20dp"
        android:layout_marginBottom="3dp"
        android:src="@drawable/username" />

How do I go from defining the images in my XML to defining them programatically so that I can try playing around with BitMapFactory in hopes that it will eliminate my OOM exception?

来源:https://stackoverflow.com/questions/11160612/android-images-from-xml-layout-to-bitmaps-in-code-to-solve-oom-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!