简体   繁体   中英

How to load html from asset folder into webview

I store my html page in asset folder and insert it to my sqlite. i store my html page as text in sqlite.

Anyone know how to load my html page into 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 +?

In any case, the method loadDataWithBaseURL takes 5 arguments:

base, data, mimetype, encoding, historyUrl

Eg:

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

readAssetFileAsString is as follows:

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 "";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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