安卓loadIcon产生的OutOfMemoryErrorloadIcon、OutOfMemoryError

2023-09-06 23:01:15 作者:我超可爱

我只是有一点点的ListView 包含所有安装的应用程序及其图标 但是,如果没有安装太多应用程序我碰到 OutOfMemoryError异常而做

I just have a little ListView containing all installed apps and their icons but if there are too much Apps installed i run into outOfMemoryErrors while doing

Drawable app_icon = applicationInfoList.get(i).loadIcon(context.getPackageManager()));

对于每个一个ListEntry (这条线是写在我的 ListAdapter

that for every ListEntry (this line is written in my ListAdapter)

没问题的,到目前为止,我明白了为什么我遇到了这个错误(太多的图标加载,过少的VM堆) 但我看了一下机器人的源$ C ​​$ C ManageApplications 活动的设置 我从来没有在任何这些运行的 OOM错误在使用设置 ManageApps 但奇怪的是,他们这样做的完全相同的方式

no problem so far, i understand why i ran into this error (too much icons loaded, too few vm heap) but i had a look at the source code of androids ManageApplications Activity in the settings and i never run in any of those oom errors while using the settings ManageApps but the weird thing is that they do it EXACTLY THE SAME WAY

,你可以在这里它们的来源看: 的 ManageApplications 活动 其中的可绘制图标被加载 当在列表视图设置

as you can see in their source here: the ManageApplications activity where the Drawable icon gets loaded where it is set in the list view

但我真的不明白为什么我得到一个OOM,他们不?

but i really dont understand why do i get a oom and they dont?

推荐答案

解决它自己。 我现在每加载图像到缓存中使用它之前。 如果缓存已满,最旧的元素将被丢弃, 因此新的可获取缓存。 此外即时调整我的图片为较小的图标。 该做的。

Solved it myself. I'm now loading every image into a cache before using it. If the cache gets full, the oldest element will be discarded, so new ones can get cached. additionally im resizing my images into smaller icons. that did it.