2017-09-08 65 views
0

加載圖像(web_image)我試圖加載一些HTML字符串是這樣的:如何在Android的WebView中

<p>外包訊息:<a href="http://waibao.io/money/59b0ae3c1d41c85f4990c524">實現一個微信小程序,費用 4000-6000</a></p> 
 

 
<p><img alt="" src="http://ww1.sinaimg.cn/large/610dc034ly1fjaxhky81vj20u00u0ta1.jpg" /></p> 
 

 
<h3>iOS</h3> 
 

 
<ul> 
 
\t <li><a href="https://github.com/Kofktu/KUIPopOver" target="_blank">實用好看的浮動窗體</a>&nbsp;(S) 
 

 
\t <ul> 
 
\t </ul> 
 
\t </li> 
 
</ul> 
 

 
<h3>Android</h3> 
 

 
<ul> 
 
\t <li><a href="http://skyseraph.com/2017/06/04/Android/UiAutomator2.0%E5%8D%87%E7%BA%A7%E5%A1%AB%E5%9D%91%E8%AE%B0/" target="_blank">UiAutomator2.0升級填坑記</a>&nbsp;(SkySeraph) 
 

 
\t <ul> 
 
\t </ul> 
 
\t </li> 
 
\t <li><a href="https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&amp;mid=2247486650&amp;idx=1&amp;sn=9c6e2e0fd5780aab2bcb02b823e7316e" target="_blank">Scrum:官僚者們的遊戲</a>&nbsp;(陳宇明) 
 
\t <ul> 
 
\t </ul> 
 
\t </li> 
 
\t <li><a href="https://mp.weixin.qq.com/s?__biz=MzIwMzYwMTk1NA==&amp;mid=2247486825&amp;idx=1&amp;sn=db596ed4b69bd9220f6a8ad79ef308db" target="_blank">關於TCP/IP,必知必會的十個問題</a>&nbsp;(陳宇明) 
 
\t <ul> 
 
\t </ul> 
 
\t </li> 
 
</ul> 
 

 
<h3>前端</h3> 
 

 
<ul> 
 
\t <li><a href="https://zhuanlan.zhihu.com/p/29050512" target="_blank">前端每週清單:Web 現狀分析與優化策略、Vue 單元測試、Headless Chrome 爬蟲</a>&nbsp;(王下邀月熊(Chevalier)) 
 

 
\t <ul> 
 
\t </ul> 
 
\t </li> 
 
\t <li><a href="https://zhuanlan.zhihu.com/p/29116364" target="_blank">JavaScript Event Loop 機制詳解與 Vue.js 中實踐應用</a>&nbsp;(王下邀月熊(Chevalier)) 
 
\t <ul> 
 
\t </ul> 
 
\t </li> 
 
\t <li><a href="https://github.com/wonderunit/storyboarder" target="_blank">開源的電影故事板,做的超級棒。</a>&nbsp;(代碼家) 
 
\t <ul> 
 
\t \t <li><a href="https://github.com/wonderunit/storyboarder" target="_blank"><img src="http://img.gank.io/46366993-4210-466f-9dd0-4412504bb911" title="開源的電影故事板,做的超級棒。" /></a></li> 
 
\t </ul> 
 
\t </li> 
 
</ul> 
 

 
<h3>休息視頻</h3> 
 

 
<ul> 
 
\t <li><a href="http://www.bilibili.com/video/av13778285/" target="_blank">《爐石傳說》是如何唱出來的</a>&nbsp;(LHF) 
 

 
\t <ul> 
 
\t </ul> 
 
\t </li> 
 
</ul> 
 

 
<p>感謝所有默默付出的編輯們,願大家有美好一天。</p>

你可以看到,有兩個知情同意在代碼中。當我在網絡視圖中添加這個網站,它只是不能顯示PIC和GIF只是看着像這樣: enter image description here

我搜索了一些方法,但沒有工作:

這是我的代碼。誰能幫忙?

contentWebView = (WebView) findViewById(R.id.detail_content_wv); 
//  Uri myUri = Uri.parse(img); 
//  titleImg.setImageURI(myUri); 

//  html = String.format(HTML_FORMAT, img); 

WebSettings webSettings = contentWebView.getSettings(); 
webSettings.setJavaScriptEnabled(true); 
webSettings.setDefaultFixedFontSize(13); 
webSettings.setDefaultTextEncodingName("UTF-8"); 
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); 
webSettings.setUseWideViewPort(true); 
webSettings.setDomStorageEnabled(true); 
webSettings.setSupportZoom(true); 
webSettings.setSaveFormData(false); 

contentWebView.loadDataWithBaseURL(null, HTML_FORMAT, "text/html", "UTF-8", null); 

這:

private void localHtmlImage() { 
     // TODO Auto-generated method stub 
     try { 
      // SDK1.5本地文件處理(不能顯示圖片) 
      // webView.loadData(URLEncoder.encode(data, encoding), mimeType, 
      // encoding); 
      // SDK1.6及以後版本 
      contentWebView.loadData(HTML_FORMAT, "text/html", "utf-8"); 
      // 本地文件處理(能顯示圖片) 
      contentWebView.loadDataWithBaseURL(null, HTML_FORMAT, "text/html", 
        "utf-8", ""); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

回答

0

他媽的KI忘記在AndroidManifest.xml添加以下代碼: <uses-permission android:name="android.permission.INTERNET" />

0

試試這行代碼

contentWebView.getSettings().setLoadsImagesAutomatically(true); 
0
 Write this in string.xml 

     </string> 


      <string name="your_string"> 
        <![CDATA[ 
     <html> 
     <head> 
     <style> 
     .aligncenter { text-align: center; } 
     p { 
      border: 1px solid black; 
      padding 5px 
      background-color: lightblue; 
     } 

     </style> 
     </head> 
     <body style="text-align:justify;color:black;background-color:white;"> 


      <p><img src=\'{IMAGE_PLACEHOLDER_GUARD_PATROLLING}\' width="100%"/></p> 
    also write your other html code , 

     </body> 
     </html> 
     ]]> 
      </string> 


     in java file write below code 


      WebView view = (WebView)findViewById(R.id.inset_web_view); 
        view.setVerticalScrollBarEnabled(false); 
        view.getSettings().setJavaScriptEnabled(true); 

      Drawable myDrawable = getResources().getDrawable(R.drawable.your_image); 
        bitmap = ((BitmapDrawable) myDrawable).getBitmap(); 

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, byteArrayOutputStream); 
        byte[] byteArray = byteArrayOutputStream.toByteArray(); 
        String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT); 
        final String image = "data:image/png;base64," + imgageBase64; 

        String html = getResources().getString(R.string.your_string); 
        html = html.replace("{IMAGE_PLACEHOLDER_GUARD_PATROLLING}", image); 
        view.loadDataWithBaseURL("", html, "text/html", "utf-8", ""); 




also put this code in your activity.xml file 

<ScrollView 
     android:layout_width="match_parent" 
     android:layout_marginTop="5dp" 
     style="@style/scrollbar_shape_style" 
     android:layout_height="wrap_content"> 
     <WebView 
      android:id="@+id/inset_web_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
     </WebView> 
    </ScrollView> 
+0

好的,你確定你的代碼可以工作或在我的IDE錯了嗎? –

+0

bitmap =((BitmapDrawable)myDrawable).getBitmap(); –

+0

Drawable myDrawable = getResources()。getDrawable(R.drawable.your_image_id_will_be_here); –