Android的 - 将在HTML的WebView一个图像将在、图像、Android、WebView

2023-09-05 03:27:27 作者:帅裂苍穹

好了,所以我使用的图像称为test.png,我有一个Java类(Cherry.java)和XML类(cherry.xml)另外我有堪称/ RES /原始文件夹中的HTML文件htmltest.html。当用户点击了previous页面上的按钮,然后将他们带到cherry.xml所有它是一个web视图我想要做的是。现在,在Java类的刚刚开放的htmltest文件和HTML文件仅仅是一个正常的基于网络的布局。我想在HTML文件中显示图像,以便在绘制文件夹或类似的东西与出不必使用互联网的图像多数民众赞成。 (不希望使用互联网许可)。下面是code为3个文件我。

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.

cherry.xml

cherry.xml

<WebView 
    android:id="@+id/webviewHelp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"  />

Cherry.java

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

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.

推荐答案

创建这个目录资产/ WWW

然后把 WWW 文件夹内的HTML和图像等。 并使用以下code。

then place your html and image etc inside the www folder. And use the following code.

Cherry.java

Cherry.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" />