在Android中加载本地html页面的Webview很慢

时间:2012-04-03 08:34:17

标签: android android-webview

您好我在webchromeclient处理一些javascript警报后加载本地html文件, 但是在我调用webview的loadUrl方法后,我的本地html页面加载非常缓慢,它等待大约20秒加载。

以下是我的代码:

@Override
    public boolean onJsAlert(WebView view, String url, String message,
            JsResult result) {
        // TODO Auto-generated method stub

        result.confirm();

        if (message.contains(GeneralConstants.ALERT_LOGIN_TIMUSER)) {
            String s[] = message.split(";");

            //Set ldap user 


            view.loadUrl("file:///android_asset/mainMenu.html");

            return true;
        }

感谢您的任何建议。

1 个答案:

答案 0 :(得分:7)

尝试这段代码以获得更好的性能

    AssetManager mgr = getContext().getAssets();
                 try {
                     InputStream in = mgr.open(FileName,AssetManager.ACCESS_BUFFER);

                     String sHTML = streamToString(in);
                     in.close();

                     //display this html in the browser
                     WebView w = (WebView) findViewById(R.id.webview);
                     w.getSettings().setDefaultZoom(ZoomDensity.FAR);
                     w.loadDataWithBaseURL("file:///android_asset/", sHTML, "text/html", "utf-8", null);                        

                 } catch (IOException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
                 }



public static String StreamToString(InputStream in) throws IOException {
        if(in == null) {
            return "";
        }

        Writer writer = new StringWriter();
        char[] buffer = new char[1024];

        try {
            Reader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));

            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        } finally {

        }

        return writer.toString();
    }