Webview内存不足错误

时间:2015-07-27 09:13:41

标签: android webview out-of-memory

我正在尝试将html数据加载到大多数时间都可以工作的webview上,但偶尔会有很长的内容,当我尝试加载数据时会抛出内存不足异常。

这是logcat;

java.lang.OutOfMemoryError
       at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
       at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
       at java.lang.StringBuilder.append(StringBuilder.java:216)
       at com.radioafrica.mpasho.view.EntryView.generateHtmlContent(EntryView.java:195)
       at com.radioafrica.mpasho.view.EntryView.setHtml(EntryView.java:158)
       at com.radioafrica.mpasho.view.ArticleView.setArticleContent(ArticleView.java:212)
       at com.radioafrica.mpasho.view.ArticleView.setContent(ArticleView.java:142)
       at com.radioafrica.mpasho.fragment.EntryFragment$EntryPagerAdapter.displayEntry(EntryFragment.java:659)
       at com.radioafrica.mpasho.fragment.EntryFragment.onLoadFinished(EntryFragment.java:546)
       at com.radioafrica.mpasho.fragment.EntryFragment.onLoadFinished(EntryFragment.java:71)
       at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
       at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
       at android.support.v4.content.Loader.deliverResult(Loader.java:104)
       at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
       at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
       at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
       at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
       at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
       at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
       at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:5283)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
       at dalvik.system.NativeStart.main(NativeStart.java)

这就是我加载数据的方式:

 public void setHtml(long entryId, String contentText, String link) {
    if (PrefUtils.getBoolean(PrefUtils.DISPLAY_IMAGES, true) || !PrefUtils.getBoolean(PrefUtils.LITE_VERSION, false)) {
        contentText = HtmlUtils.replaceImageURLs(contentText, entryId);
        if (getSettings().getBlockNetworkImage()) {
            // setBlockNetwortImage(false) calls postSync, which takes time, so we clean up the html first and change the value afterwards
            loadData("", TEXT_HTML, Constants.UTF8);
            getSettings().setBlockNetworkImage(false);
        }
    } else {
        contentText = contentText.replaceAll(HTML_IMG_REGEX, "");
        getSettings().setBlockNetworkImage(true);
    }

    loadDataWithBaseURL("https://twitter.com/", generateHtmlContent(contentText, link), TEXT_HTML, Constants.UTF8, null);

private String generateHtmlContent(String contentText, String link) {
    String s[] = link.split("/");
    String identifier = s[s.length - 1];

    String comments = "<div id=\"disqus_thread\"></div>\n" +
            "\t<script type=\"text/javascript\">\n" +
            "\tvar disqus_shortname = 'mpashonews';\n" +
            "\n" +
            "\tvar disqus_identifier = '" + identifier + "';\n" +
            "\tvar disqus_title = 'Comments';\n" +
            "\t(function() {\n" +
            "\tvar dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;\n" +
            "\tdsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';\n" +
            "\t(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);\n" +
            "\t})();\n" +
            "\t</script>\n" +
            "\t<noscript>Please enable JavaScript to view the <a href=\"https://disqus.com/?ref_noscript\" rel=\"nofollow\">comments powered by Disqus.</a></noscript>";

    return CSS + BODY_START + contentText + "<br/>" + comments + BODY_END;
}

加载数据时处理大字符串的最佳方法是什么?

0 个答案:

没有答案