webview 加载带有图片的html 文件

两种方式:

方式一:

  1. String htmlContent = StringEscapeUtils.unescapeJava(data);  
  2.   
  3. // String m = "src=";  
  4. // htmlContent = htmlContent.replaceAll(m, "src=\"" + Constants.IMAGE_URL);  
  5.   
  6. Document doc = Jsoup.parse(htmlContent);  
  7.   
  8. Elements elem_img = doc.getElementsByTag("img");  
  9. // 图片自适应屏幕  
  10. if (elem_img.size() != 0) {  
  11. for (Element el_img : elem_img) {  
  12. el_img.attr("style""width:100%");  
  13. }  
  14. }  
  15. htmlContent = doc.toString();  
  16.   
  17. webview.loadData(htmlContent, "text/html; charset=UTF-8"null);  
  18.   
  19. webview.getSettings().setDefaultTextEncodingName("UTF-8");// 设置默认为utf-8   
方式二:

  1. String html = response.getContent();  
  2. if (html.contains("src=\"/public")){  
  3.     html = html.replace("src=\"/public""src="+"\""+Config.WEB_HOST+"\""+"+\"/public");  
  4. }  
  5. showWebView(html);  
  6. private void showWebView(String html)  
  7. {  
  8.     // 设置WevView要显示的网页  
  9.     webviewArticleContent.getSettings().setJavaScriptEnabled(true);//设置支持Javascript  
  10.     webviewArticleContent.getSettings().setBlockNetworkImage(false);  
  11.     webviewArticleContent.requestFocus();//触摸焦点起作用.如果不设置,则在点击网页文本输入框时,不能弹出软键盘及不响应其他的一些事件。  
  12.     webviewArticleContent.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);  
  13.     if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP){  
  14.         webviewArticleContent.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);  
  15.     }  
  16.     //        luntanListview.getSettings().setBuiltInZoomControls(true); //页面添加缩放按钮  
  17.     //                luntanListview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);   //取消滚动条  
  18.   
  19.     //                点击链接由自己处理,而不是新开Android的系统browser响应该链接。  
  20.     webviewArticleContent.setWebViewClient(new WebViewClient(){  
  21.         @Override  
  22.         public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){  
  23.             //注意:super句话一定要删除,或者注释掉,否则又走handler.cancel()默认的不支持https的了。  
  24.             //super.onReceivedSslError(view, handler, error);  
  25.             //handler.cancel(); // Android默认的处理方式  
  26.             //handler.handleMessage(Message msg); // 进行其他处理  
  27.   
  28.             handler.proceed(); // 接受所有网站的证书  
  29.         }  
  30.   
  31.         @Override  
  32.         public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  33.             view.loadUrl(url);  
  34.             return true;  
  35.         }  
  36.     });  
  37.     String CSS_STYPE = "<head><style>img{max-width:100% !important;} table{max-width:100% !important;}</style></head>";  
  38.     webviewArticleContent.loadDataWithBaseURL(null, CSS_STYPE + html, "text/html""utf-8"null);  
  39.     //        luntanListview.setOnFocusChangeListener(new View.OnFocusChangeListener() {  
  40.     //            @Override  
  41.     //            public void onFocusChange(View v, boolean hasFocus) {  
  42.     //                if (hasFocus) {  
  43.     //                    try {  
  44.     //                        // 禁止网页上的缩放  
  45.     //                        Field defaultScale = WebView.class  
  46.     //                                .getDeclaredField("mDefaultScale");  
  47.     //                        defaultScale.setAccessible(true);  
  48.     //                        defaultScale.setFloat(luntanListview, 1.0f);  
  49.     //                    } catch (SecurityException e) {  
  50.     //                        e.printStackTrace();  
  51.     //                    } catch (IllegalArgumentException e) {  
  52.     //                        e.printStackTrace();  
  53.     //                    } catch (IllegalAccessException e) {  
  54.     //                        e.printStackTrace();  
  55.     //                    } catch (NoSuchFieldException e) {  
  56.     //                        e.printStackTrace();  
  57.     //                    }  
  58.     //                }  
  59.     //            }  
  60.     //        });  
  61. }  

猜你喜欢

转载自blog.csdn.net/Charles_dai001/article/details/79127966
今日推荐