Android - 在html webview中添加图像

Pie*_*Pie 3 html android android-webview

好的,我正在使用的图像名为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

<WebView 
    android:id="@+id/webviewHelp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  />
Run Code Online (Sandbox Code Playgroud)

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();
    }



}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

再问任何问题,我会尽快回复.一切都很好,它只是我无法显示的图像.

Rah*_*tel 9

首先欢迎来到stackoverflow.

现在将您的html和图像等放在Assets文件夹中.并使用以下代码.

Cherry.java

WebView webview = (WebView) findViewById(R.id.abtus_webView);
webview.loadUrl("file:///android_asset/index.html");
Run Code Online (Sandbox Code Playgroud)

htmltest.html

<img src="images/abc.png">
Run Code Online (Sandbox Code Playgroud)

我已将图像放入Assets文件夹中的图像文件夹.这对我来说是正确的,我希望它能帮到你.