WebView 中的大图像导致内存不足

发布于 2024-12-02 03:48:38 字数 760 浏览 1 评论 0原文

我有一个活动可以解析 XML feed(带有新闻)并在 WebView 中加载解析的数据(文本和图像 url),这些数据位于图库小部件内。

像这样的事情:

mimeType = "text/html";

encoding = "utf-8";

String html = "<img src=\""
+ newsImageUrl.get(position)
+ "\" style=\"max-width:200px; max-height:200px;\" align=\"left\""
+ "/>" + newsDescription.get(position);

data.loadDataWithBaseURL("", html, mimeType, encoding, "");

一切正常,但有时在新闻提要中会有这样的大图像。好吧,除非您开始旋转手机,否则它们不会造成任何问题。经过几次方向更改后,我们遇到了内存不足异常。

好吧,在 Android 中,OOM 总是隐藏在 Bitmas 附近的某个地方,这就是为什么大多数人将 BitmapFactory 与 inSampleSize 一起使用,或者更奇特的东西。嗯,据说,我可以下载并重新采样图像,然后从 SD 卡加载它们。但现在我会尽力避免它。

无论如何,问题是 - 当大图像加载到 WebView 中时该怎么办?有没有办法调整它们的大小(不仅仅是视觉上)?有什么方法可以清除 WebView onOrientationChange 占用的内存(webview.freeMemory() 并没有真正帮助)。

I have activity that parses XML feed (with news) and loads parsed data (text and image url) in WebView's, which are inside gallery widget.

Something like this:

mimeType = "text/html";

encoding = "utf-8";

String html = "<img src=\""
+ newsImageUrl.get(position)
+ "\" style=\"max-width:200px; max-height:200px;\" align=\"left\""
+ "/>" + newsDescription.get(position);

data.loadDataWithBaseURL("", html, mimeType, encoding, "");

Everything works fine, but sometimes inside news feed there is this BIG IMAGES. Well, they dont cause any problems unless you start to rotate a phone. And after couple of orientation changes we have happy Out Of Memory exception.

Well, in Android, OOM is always hiding somewhere near Bitmas, that's why most people use BitmapFactory with inSampleSize, or something more exotic. Well, supposedly, I can download and resample images, and then just load them from SD card. But for now I will try to avoid it.

Anyway, the question is - what to do with large images when they are being loaded in WebView? Is there a way to resize them (not only visually)? Is there any way to clear memory occupied by WebView onOrientationChange (webview.freeMemory() doesn't really helps).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绅刃 2024-12-09 03:48:38

事实上,你没有那么多选择。释放内存并没有帮助,因为您的图像仍然分配在内存中,并且图像太大而无法容纳。

唯一可行的方法是在显示它们之前在源头(这样您无需以编程方式执行任何操作)或在下载时减小它们的大小(将它们按比例保存到 SD 并显示这些本地副本而不是源图像)。

这取决于您是否必须在某个时候使用全尺寸图像。如果不是,那么将它们保持这么大绝对没有意义。

Actually, you don't have so many choices. Freeing memory doesn't help as your images are still allocated in memory, and the images are too big to fit.

The only workable way is to reduce their size, either at the source (this way you have nothing to do programmatically) or at download time, before you display them (you save them scaled down to the SD and you display these local copies instead of the source images).

This depends of whether you have to use the full-scale images at some point. If not, there's absolutely no point in keeping them so large.

一瞬间的火花 2024-12-09 03:48:38

无论如何,问题不在于 webview 大图像。

我缩小了应用程序的规模,显然问题出在我的画廊小部件设计中。更具体地说是它周围的图形界面元素。即使在几个方向改变后画廊空了,我也得到了 oom。

我直到今天才找到原因。我使用 /drawables/ 文件夹将所有图像缩小为 mdpi。我测试过的所有手机都是 hdpi。显然,android 使用了一些可能不那么有效的缩放方法,并且它在某处泄漏。

我将所有绘图移至 hdpi 和 mdpi 文件夹,崩溃神奇地停止了。

所以,结论 - 避免使用 /drawables/ 文件夹。

Anyway, the problem was not in webview large images.

I downscaled app and apparently the problem was in my gallery widget design. To be more specific in graphical interface elements that surrounds it. Even with empty gallery after couple orientation changes I got oom.

And I found the reason to it only today. I used /drawables/ folder which downscales all images to mdpi. And all phones I tested it on was hdpi. Apparently android uses some maybe no so efficient scaling method, and it's leaking somewhere there.

I moved all my drawables to hdpi and mdpi folders and crashes magically stopped.

So, conclusion - avoid using /drawables/ folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文