在WebView中大的图像导致内存不足 [英] Large images in WebView cause Out Of Memory

查看:1054
本文介绍了在WebView中大的图像导致内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个解析XML饲料(新闻)活性和负荷分析数据的WebView,这是画廊内的部件(文字和图片的URL)。

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.

事情是这样的:

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.

那么,在Android中,OOM总是藏在什么地方Bitmas附近,这就是为什么大多数人使用BitmapFactory与inSampleSize,或更多的东西异国情调。好了,按说,我可以下载并重新取样图像,然后刚刚从SD卡中加载它们。但现在我会尽量避免。

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.

总之,问题是 - 大图片做什么,当他们在WebView中被加载?有没有一种方法来调整它们的大小(不仅在视觉上)?有什么办法,以清除的WebView onOrientationChange占用的内存(webview.freeMemory()并没有真正帮助)。

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).

推荐答案

其实,你没有这么多的选择。释放内存没有帮助的图像在内存中分配仍在,图像过大,以适应。

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.

唯一可行的方法就是减少它们的大小,无论是在源(这样你有什么编程方式),或在下载时,你显示它们之前(您保存它们缩小到SD,你显示这些地方副本,而不是源图像)。

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.

这篇关于在WebView中大的图像导致内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆