Android,为什么我的html文件没有显示?

时间:2011-05-17 13:45:54

标签: android html view

我已将部分信息存储在HTML文件中,并将其放入资产文件夹中。但是,当我想要显示它时,网页会启动,但它会说“网页不可用”,而我已将其存储在资源文件夹中。

这是我的代码:

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.am_culture); 

        final FrameLayout frame01 = (FrameLayout) findViewById(R.id.ch_frame01);
        final WebView wv = new WebView(this);

        frame01.setOnClickListener(new View.OnClickListener() {         
            @Override
            public void onClick(View v) {
                wv.loadUrl("file:///android_asset/people.html");
                setContentView(wv);
            }
        });
    }

enter image description here

2 个答案:

答案 0 :(得分:0)

    WebView mWebView = null;
    mWebView = (WebView) findViewById(R.id.wb1);
    mWebView.getSettings().setJavaScriptEnabled(true);
    InputStream is;
    try {
        is = getAssets().open("people.html");
        int size = is.available();
        // Read the entire asset into a local byte buffer.
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        // Convert the buffer into a string.
        String strContent = new String(buffer);
        mWebView.loadData(strContent, "text/html", "UTF-8");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 1 :(得分:0)

这应该是您检索资产文件的代码

public String inputStreamToString()
{
    StringBuffer sBuffer = new StringBuffer();
    try
    {
        InputStream is = getResources().getAssets().open("people.html");

        byte[] b = new byte[1024];
        for (int n; (n = is.read(b)) != -1;) {
            sBuffer.append(new String(b, 0, n));
        }
    }
    catch (Exception e) {
        // TODO: handle exception
        //Log.e("inputstream",e.toString());
    }

    return sBuffer.toString();
}     

public void webview()
{
    WebView wb = (WebView) findViewById(R.id.webView1);
    wb.getSettings().setJavaScriptEnabled(true);

    String summary = inputStreamToString();
    wb.loadData(summary, "text/html", "utf-8");
}