简体   繁体   English

如何将资产文件夹中的html加载到webview中

[英]How to load html from asset folder into webview

I store my html page in asset folder and insert it to my sqlite.我将我的 html 页面存储在资产文件夹中并将其插入到我的 sqlite 中。 i store my html page as text in sqlite.我将我的 html 页面作为文本存储在 sqlite 中。

Anyone know how to load my html page into webview?任何人都知道如何将我的 html 页面加载到 webview 中?

i've tried to add some code, and not working我试图添加一些代码,但不起作用

    if (info.size() != 0) {
        lu.setText(info.get(2));
        WebView wb = (WebView) findViewById(R.id.mywebview);
        wb.loadDataWithBaseURL("file:///android_asset/"+lu,"text/html","UTF-8",null);
    }

您的问题已经在 SO Webview 从资产目录加载 html上得到了详尽的答案……我相信其中一个答案应该可以解决您的问题……希望对 gudluck 有所帮助。

what is lu?, should it be a comma instead of +?什么是lu?,应该是逗号而不是+?

In any case, the method loadDataWithBaseURL takes 5 arguments:在任何情况下,方法loadDataWithBaseURL需要 5 个参数:

base, data, mimetype, encoding, historyUrl基础、数据、mimetype、编码、historyUrl

Eg:例如:

wbHelp.loadDataWithBaseURL("file:///android_asset/", 
readAssetFileAsString("index.html"), 
"text/html", 
"UTF-8", 
null);

readAssetFileAsString is as follows: readAssetFileAsString 如下:

private String readAssetFileAsString(String sourceHtmlLocation)
{
    InputStream is;
    try
    {
        is = getContext().getAssets().open(sourceHtmlLocation);
        int size = is.available();

        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();

        return new String(buffer, "UTF-8");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }

    return "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM