我可以在android webview中显示HTML文件内容.现在我怎样才能将参数传递给HTML文件.对于ex.my HTML内容有一个视频播放器我需要将动态值(URL)传递到HTML文件中以播放动态视频.我的HTML文件位于资产文件夹中.我怎么能这样做?
谢谢.
我今天遇到了这个问题,但是我需要这个用UTF-8编码,所以这是我的方法,希望它能帮助某人并澄清以前对这个问题的一些答案.
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<h1>%ERR_TITLE%</h1>
<h2>%ERR_DESC%</h2>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Java的:
String content = IOUtils.toString(getAssets().open("error.html"))
.replaceAll("%ERR_TITLE%", getString(R.string.error_title))
.replaceAll("%ERR_DESC%", getString(R.string.error_desc))
mWebView.loadDataWithBaseURL("file:///android_asset/error.html", content, "text/html", "UTF-8", null);
Run Code Online (Sandbox Code Playgroud)
至于IOUtils:http://commons.apache.org/proper/commons-io/download_io.cgi
我不会直接传递视频 URL(按照您的示例),而是在 Html 文件中使用令牌。例如:
<embed src="$VIDEO_URL$" autostart="false" />
Run Code Online (Sandbox Code Playgroud)
其中 是$VIDEO_URL$在运行时将被真实视频替换的令牌URL。
另外,由于您无法在运行时更改 asset 文件夹的内容,因此您应该将 html 文件内容加载到 String 变量中,并使用该方法replace将令牌替换为真实的令牌URL,最后将该字符串传递给您的 webview。像这样的东西:
//The html variable has the html contents of the file stored in the assets folder
//and real_video_url string variable has the correct video url
html = html.replace("$VIDEO_URL$", real_video_url);
webview.loadData(html, "text/html", "utf-8");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10158 次 |
| 最近记录: |