Android 开发:在 WebView 的 HTML 中使用资产中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3779789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Development: Using Image From Assets In A WebView's HTML
提问by AlexPriceAP
In my app I'm making a basic HTML help document. I wanted my app's logo in the HTML img tag itself, but I don't know how I'd reference to the logo which will be stored in assets.
在我的应用程序中,我正在制作一个基本的 HTML 帮助文档。我希望在 HTML img 标签本身中包含我的应用程序徽标,但我不知道如何引用将存储在资产中的徽标。
Is this possible, if so how?
这是可能的,如果可以,如何?
Thanks for the help!
谢谢您的帮助!
回答by spatialist
Put your Logo into the assets directory eg: assets/logo.png
将您的徽标放入资产目录中,例如:assets/logo.png
Then load your html with:
然后加载你的html:
webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "utf-8", null);
Reference your img like:
引用您的 img,如:
<img src="logo.png">
回答by Jorgesys
Store the images in assets folder:
将图像存储在资产文件夹中:
Read the image in the html from assets with file:///android_asset/
从资产中读取 html 中的图像 file:///android_asset/
for example:
例如:
String sHtmlTemplate = "<html><head></head><body><img src=\"file:///android_asset/img/mybadge.png\"></body></html>";
load inside the WebView:
在 WebView 中加载:
webView.loadDataWithBaseURL(null, sHtmlTemplate, "text/html", "utf-8",null);
回答by Record413123
dump them into assets folder and just put in the html
将它们转储到资产文件夹中,然后放入 html
<img src="filename.png">
simple as that
就那么简单
回答by Robert Nekic
You can reference assets with this URL syntax:
您可以使用以下 URL 语法引用资产:
file:///android_asset/YourAssetFilename
回答by user2488738
Put your Logo into the assets directory Example : assets/logo.png
将您的徽标放入资产目录示例:assets/logo.png
Then
然后
String imgData="<img src=home.png>";
webView.loadDataWithBaseURL("file:///android_asset/", imgData, "text/html", "utf-8", null);