Android - 在 html webview 中添加图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10843178/
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 - adding a image in html webview
提问by Pie
Ok so the image i'm using is called test.png and I have a java class (Cherry.java) and a xml class (cherry.xml) Also I have a html file in the /res/raw folder called htmltest.html. What i'm trying to do is when the user clicks a button on the previous page and then takes them to cherry.xml all it is a webview. Now in the java class its just opening up the htmltest file and in the html file is just a normal web based layout. I want to display images in the html file so a image thats in the drawable folder or something like that with out having to use the internet. (don't want the internet permission to be used). Below is the code for the 3 files I have.
好的,所以我使用的图像称为 test.png,我有一个 java 类 (Cherry.java) 和一个 xml 类 (cherry.xml) 另外我在 /res/raw 文件夹中有一个名为 htmltest.html 的 html 文件. 我想要做的是当用户单击上一页上的按钮然后将它们带到cherry.xml 时,它就是一个webview。现在在 java 类中它只是打开 htmltest 文件,而在 html 文件中只是一个普通的基于 web 的布局。我想在 html 文件中显示图像,以便在 drawable 文件夹中显示图像或类似的图像,而不必使用互联网。(不希望使用互联网权限)。以下是我拥有的 3 个文件的代码。
cherry.xml
樱桃.xml
<WebView
android:id="@+id/webviewHelp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Cherry.java
樱桃.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Cherry extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cherry);
WebView webview = (WebView) findViewById(R.id.webviewHelp);
webview.loadData(readTextFromResource(R.raw.htmltest), "text/html", "utf-8");
}
private String readTextFromResource(int resourceID)
{
InputStream raw = getResources().openRawResource(resourceID);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int i;
try
{
i = raw.read();
while (i != -1)
{
stream.write(i);
i = raw.read();
}
raw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return stream.toString();
}
}
htmltest.html
htmltest.html
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<h2>Pictures</h2>
<img border="0" src="file:///android_drawable/test.png" alt="nothing" width="304" height="228" />
</body>
</html>
Any more questions just ask and i'll reply as fast as possible. Everything works fine its just the images that I can't get to display.
如有更多问题,请提出,我会尽快回复。一切正常,只是我无法显示的图像。
采纳答案by root
Create this directory assets/www
创建这个目录 assets/www
then place your html and image etc inside the www
folder.
And use the following code.
然后将您的 html 和图像等放在www
文件夹中。并使用以下代码。
Cherry.java
樱桃.java
webview.loadUrl("file:///android_asset/www/htmltest.html");
htmltest.html
htmltest.html
<img border="0" src="../res/drawable-hdpi/test.png" alt="nothing" width="304" height="228" />
回答by Rahul Patel
First of all welcome to stackoverflow.
首先欢迎来到stackoverflow。
Now put your html and image etc inside the Assets folder. And use the following code.
现在将您的 html 和图像等放在 Assets 文件夹中。并使用以下代码。
Cherry.java
樱桃.java
WebView webview = (WebView) findViewById(R.id.abtus_webView);
webview.loadUrl("file:///android_asset/index.html");
htmltest.html
htmltest.html
<img src="images/abc.png">
I have put the images into the images Folder in Assets folder.This is working for me correct,I hope it helps you.
我已将图像放入 Assets 文件夹中的图像文件夹中。这对我来说是正确的,希望对您有所帮助。
回答by user3108698
If your images are small. Convert them to Base64 codings.
如果你的图片很小。将它们转换为 Base64 编码。
Explained here: http://dean.edwards.name/my/base64-ie.html
回答by Sreerag S Kumar
Just modify the image path
修改图片路径即可
From:
从:
file:///android_drawable/test.png
file:///android_drawable/test.png
To:
到:
file:///android_res/drawable/test.png
file:///android_res/drawable/test.png