安卓明确的WebView线程,释放内存,避免OutOfMemoryError异常 [英] Android clear webview thread, free memory, avoid OutOfMemoryError

查看:1628
本文介绍了安卓明确的WebView线程,释放内存,避免OutOfMemoryError异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个web视图显示了相当大的图像,这样已经可以了双指缩放功能和其他便利的浏览。

I am displaying a fairly large image in a webview so that the pinch-to-zoom functions and other conveniences for viewing are already available.

它会显示正确的第一次。但在离开活动,回来后,在一个OutOfMemoryError应用程序崩溃相关的WebView线程。

It displays correctly the first time. But after leaving the activity and coming back to it, the app crashes on an OutofMemoryError related to the webview thread.

我已经尝试了好东西,尝试关闭web视图或停止线程,或清除其使用的内存,但无济于事。这里是我加入到我的活动的onStop()功能

I have attempted several things to try to close the webview or stop the thread, or clear its use of memory, but to no avail. Here are examples of code that I added to my activities' onStop() function

    wv.stopLoading();
    wv.clearCache(true);
    //removeView(wv);
    wv.clearView();
    wv.freeMemory();
    wv.destroy();
    try {
        Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(wv, (Object[]) null);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

反正随意批评任何的code的线,但问题是,他们没有影响的错误了!它做的webviewcore线程本身不释放内存。

Anyway feel free to criticize any of those lines of code, but the point is that they have no influence on the error at all! It has to do with the webviewcore thread itself not releasing memory.

这是令人担忧的我,因为即使我使用的是较小的图像,这看起来像它仍然最终会发生。帮助?

This is alarming to me because even if I was using a smaller image, this looks like it would still eventually happen. Help?

推荐答案

好了,这样的结论,这个问题是该字符串缓冲区不可能是$ P $这些文件大小pdetermined或正确清除。

Okay, so the conclusion to this issue was that the String buffer could not be predetermined or properly cleared at these file sizes.

在这种情况下,将图像从一个服务器作为字符串拉出,操纵作为字符串和最终显示为图像。

In this case the image was pulled from a server as a string, manipulated as a string and finally displayed as an image.

无论是显示为ImageView的或作为web视图,它没有问题,应用程序会崩溃,一旦一个字符串缓冲区成为全(它不会清除,而不管我有多么的东西释放或设置为null),这是一个加密的努力。

Whether displayed as an ImageView or as a Webview, it did not matter, the app would crash once a string buffer became full (which it would not clear no matter how much stuff I freed or set to null), This was an encryption effort.

但最后只是与结算将图像存储在应用程序的资产文件夹。

but Finally just settled with storing the images in the app's assets folder.

这篇关于安卓明确的WebView线程,释放内存,避免OutOfMemoryError异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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