2017-09-08 3 views
0

Je suis en train de charger une chaîne HTML comme ceci:Comment charger l'image (web_image) dans 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>

Vous pouvez voir qu'il ya deux pic dans le code. Quand j'ajoute ce html dans la vue Web, il ne peut pas afficher l'image, et le gif ressemblait à ceci: enter image description here

J'ai cherché une méthode, mais aucun travaillé:

Ceci est mon code. Quelqu'un peut-il aider?

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

et ceci:

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(); 
     } 
    } 

Répondre

0

f ** kI oublié ajouter ce code dans le AndroidManifest.xml: <uses-permission android:name="android.permission.INTERNET" />

0

Essayez cette ligne de code

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

ok, êtes-vous sûr de votre code peut travailler ou quelque chose ne va pas dans mon Ide? –

+0

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

+0

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