【问题标题】:How to load html from asset folder into webview如何将资产文件夹中的html加载到webview中
【发布时间】:2016-09-11 00:01:45
【问题描述】:

我将我的 html 页面存储在资产文件夹中并将其插入我的 sqlite。 我将我的 html 页面作为文本存储在 sqlite 中。

有人知道如何将我的 html 页面加载到 webview 中吗?

我尝试添加一些代码,但无法正常工作

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

【问题讨论】:

  • 嘿 El_Auls,你应该让你的问题更清楚......通过提供更多详细信息来展示你如何尝试将它加载到你的 webview 中并失败
  • 我已经添加了我的代码,请帮助我T,T

标签: android html sqlite webview


【解决方案1】:

您的问题已经在 SO Webview load html from assets directory 上得到了详尽的答案...我相信其中一个答案应该可以解决您的问题...希望对您有帮助。

【讨论】:

    【解决方案2】:

    什么是lu?,应该是逗号而不是+?

    无论如何,loadDataWithBaseURL 方法需要 5 个参数:

    基础、数据、mimetype、编码、historyUrl

    例如:

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

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

    【讨论】:

    • 不应该将is.close() 语句放在finally 块内吗? (在try 块之前将is 初始化为null
    猜你喜欢
    • 1970-01-01
    • 2012-09-05
    • 2017-11-04
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多