Webview loading local html page slow in Android

后端 未结 1 938
心在旅途
心在旅途 2021-02-03 16:09

Hi i am loading local html file after handling some javascript alerts on webchromeclient, But after i call webview\'s loadUrl method my local html page loads very slowly it wai

1条回答
  •  误落风尘
    2021-02-03 16:28

    try this piece of code for a better performance

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

    0 讨论(0)
提交回复
热议问题