Skip to content Skip to sidebar Skip to footer

Android WebView Showing Blank Page For Local Files

My android WebView is showing a blank page when I try to load 'file:///android_asset/index.html' and it perfectly loads other url like 'http://twitter.com'. My activity public cla

Solution 1:

get string content from your assests file using below code

    public static String getStringFromAssets(String name, Context p_context)
    {
        try
        {
            InputStream is = p_context.getAssets().open(name);
            int size = is.available();
            byte[] buffer = new byte[size];
            is.read(buffer);
            is.close();
            String bufferString = new String(buffer);
            return bufferString;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return null;

    }

and load content using below code

String m_data = getStringFromAssets("index.html", this);
webView.loadDataWithBaseURL("file:///android_asset/", m_data, "text/html", "utf-8", null);

Hope it works for you !!


Solution 2:

Checkout if there are any errors in javascript code associated with the html page. If so android studio doesn't point out errors in js file.

I fixed the errors in the js file and it worked.


Post a Comment for "Android WebView Showing Blank Page For Local Files"