好的,我使用的图像被称为test.png,我有一个java类(Cherry.java)和一个xml类(cherry.xml)另外我有一个html文件在名为htmltest.html的/ res/raw文件夹中。我想要做的是,当用户点击上一页上的按钮,然后将它们带到cherry.xml中,所有这些都是webview。现在在java类中它只是打开htmltest文件,而在html文件中只是一个普通的基于web的布局。我想在html文件中显示图像,以便可绘制文件夹中的图像或类似的东西不必使用互联网。 (不要使用互联网许可)。以下是我拥有的3个文件的代码。Android - 在html webview中添加图像
cherry.xml
<WebView
android:id="@+id/webviewHelp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Cherry.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
<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>
任何更多的问题,只是问我会为尽快回复。 一切工作正常,它只是我无法显示的图像。
的Android 2。2 API 8 - 是什么这是运行 – Pie
PLZ请确保您的图像和HTML文件在同一文件夹中,如果图像不显示,并且图像源参考正确,请尝试将您的图像转换为'x64base' –