简体   繁体   中英

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 class MainActivity  extends ActionBarActivity  {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.webview);

        myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

        myWebView.setWebViewClient(new WebViewClient());
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        myWebView.loadUrl("file:///android_asset/index.html");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.loadUrl("file:///android_asset/index.html");
            return true;
        }
        return false;
    }
}

My index.html file :

<!DOCTYPE html>
<html>
<head>
    <title>World Tour</title>
    <!-- Sets initial viewport load and disables zooming  -->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">

    <!-- Makes your prototype chrome-less once bookmarked to your phone's home screen -->
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="mobile-web-app-capable" content="yes">

    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="css/ratchet.css">
</head>
<body>
    <header class="bar bar-footer">
        <a href="views/stats.html" data-transition="slide-out"><span class="icon icon-info pull-left" ></span></a>
        <a href="views/add.html" data-transition="slide-in"><span class="icon icon-plus pull-right" ></span></a>
        <h1 class="title">List of previous drive</h1>
    </header>

    <div class="content">
        <div class="card">
            <ul id="drives-list" class="table-view">
                <li class="table-view-divider table-view-cell" ><p class="pull-left">Comments</p><p class="pull-right">Distance</p></li>
            </ul>
        </div>
    </div>

    <script type="text/javascript" src="js/main.js" ></script>
    <script type="text/javascript" src="js/ratchet.js" ></script>
</body>
</html>

Thanks for helping !

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 !!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM