如何在Android WebView中加载图像(web_image)

时间:2017-09-08 03:55:48

标签: java android html android-webview

我试图加载一些像这样的HTML字符串:



<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>
	<li><a href="https://github.com/Kofktu/KUIPopOver" target="_blank">实用好看的浮动窗体</a>&nbsp;(S)

	<ul>
	</ul>
	</li>
</ul>

<h3>Android</h3>

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

	<ul>
	</ul>
	</li>
	<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;(陈宇明)
	<ul>
	</ul>
	</li>
	<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;(陈宇明)
	<ul>
	</ul>
	</li>
</ul>

<h3>前端</h3>

<ul>
	<li><a href="https://zhuanlan.zhihu.com/p/29050512" target="_blank">前端每周清单:Web 现状分析与优化策略、Vue 单元测试、Headless Chrome 爬虫</a>&nbsp;(王下邀月熊(Chevalier))

	<ul>
	</ul>
	</li>
	<li><a href="https://zhuanlan.zhihu.com/p/29116364" target="_blank">JavaScript Event Loop 机制详解与 Vue.js 中实践应用</a>&nbsp;(王下邀月熊(Chevalier))
	<ul>
	</ul>
	</li>
	<li><a href="https://github.com/wonderunit/storyboarder" target="_blank">开源的电影故事板,做的超级棒。</a>&nbsp;(代码家)
	<ul>
		<li><a href="https://github.com/wonderunit/storyboarder" target="_blank"><img src="http://img.gank.io/46366993-4210-466f-9dd0-4412504bb911" title="开源的电影故事板,做的超级棒。" /></a></li>
	</ul>
	</li>
</ul>

<h3>休息视频</h3>

<ul>
	<li><a href="http://www.bilibili.com/video/av13778285/" target="_blank">《炉石传说》是如何唱出来的</a>&nbsp;(LHF)

	<ul>
	</ul>
	</li>
</ul>

<p>感谢所有默默付出的编辑们,愿大家有美好一天。</p>
&#13;
&#13;
&#13;

您可以看到代码中有两张图片。当我在网页视图中添加这个html时,它只是无法显示图片,而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();
        }
    }

3 个答案:

答案 0 :(得分:0)

试试这行代码

contentWebView.getSettings().setLoadsImagesAutomatically(true);

答案 1 :(得分: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>

答案 2 :(得分:0)

f ** k.I忘了在AndroidManifest.xml中添加此代码: <uses-permission android:name="android.permission.INTERNET" />