3

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 waits about 20 seconds to load.

Here is my code below:

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

Thanks for any advice.

1 Answer 1

7

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();
    }
Sign up to request clarification or add additional context in comments.

4 Comments

:Hello , I am using your code because I load a local html file(offline) and it delays to open.So, because getCpontext() is undefined insted of "AssetManager mgr = getContext().getAssets();" , I put Context mcontext; , mcontext=this,"AssetManager mgr =this.getApplicationContext().getAssets();".Also, instead of Filename inside mgr.open() I am putting the ""file:///android_asset/hello.html" , right?But it doesn't load at all the page.If you could help me..Thanks.(I upvoted)
instead using AssetManager mgr =this.getApplicationContext().getAssets(); us AssetManager mgr =this.getAssets(); also don't give full path give only name because in loadDataWithUrl mathod we are giving path to assets
:Unfortunately the page does not load at all.In mgr.open(FileName,,... I do mgr.open("thename.htm",...),right?In order to load the page (slow) I must use "loadUrl".It doesn't work with "loadDataWithBaseURL".have you any idea?Thank you
i think there is some problem in your code could you provide your code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.