2

我想在 Android webview 中加载以下 HTML 代码。

<!DOCTYPE html>
<html>
<body style = "text-align:center">

<img src="http://XXXXXXXX.com/books_snaps/UR335/1.jpg" alt="pageNo" 

height="100%" width="100%"> 

</body>
</html>

http://XXXXXXXX.com/books_snaps/UR335/1.jpg图片链接在哪里。我想将此图像加载到<img>上述 HTML 的标记中。如何在 Android 的 webview 中嵌入 HTML 代码?

4

4 回答 4

9
String htmlString = "<!DOCTYPE html><html><body style = \"text-align:center\"><img src=\"http://shiaislamicbooks.com/books_snaps/UR335/1.jpg\" alt=\"pageNo\" height=\"100%\" width=\"100%\"></body></html>";
webview.loadDataWithBaseURL(null,htmlString,"text/html","UTF-8","about:blank");

另外,您需要添加互联网权限,

<uses-permission android:name="android.permission.INTERNET" /> 
于 2013-01-09T10:29:36.420 回答
1

您可以先将 html 文件保存在 assets 文件夹中说

assets\html\index.html

然后只需将页面加载到 webview 作为

   webView.loadUrl("file:///android_asset/html/index.html");

或者你可以试试这个

String content = 
       "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"+
       "<html><head>"+
       "<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />"+
       "<head><body>";

content += myContent + "</body></html>";

WebView WebView1 = (WebView) findViewById(R.id.webView1);
WebView1.loadData(content, "text/html; charset=utf-8", "UTF-8");
于 2013-01-09T10:33:08.683 回答
1

按照 Sanket Kachhela 的回答,如果您在 Android 9.0 设备上运行您的应用程序,但仍然看不到图像,请考虑android:usesCleartextTraffic="true"在清单文件中添加应用程序标签。

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
    ...

对我来说,它完成了这项工作。

看到这个线程

于 2018-12-27T07:11:45.183 回答
0

您可以从链接下载该图像,然后您需要在您的 android 应用程序资产文件夹中创建一个名为“Images”的文件夹。

然后在您可以将图像链接的路径更改为../images/1.jpghtml 文件之后。

试试这个方法。我相信它会起作用。

谢谢。

于 2013-01-09T10:32:41.523 回答