Question

I am an Android newbie and have been futzing around with trying to render an html file in my assets folder and getting a blank webView screen when my app runs in the emulator. Here is my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView = (WebView) findViewById(R.id.webview);
    WebView.getSettings().setJavaScriptEnabled(true);
    WebView.setWebChromeClient(new WebChromeClient());

    String filePath = "file///android_asset/www/index.html";
    //String filePath = "http://www.php.net/manual/en/imagick.getimageregion.php";
    File file = new File(filePath);

    if(file.exists()){
        Log.d("WebView",filePath);
    }
    Log.d("WebView",filePath);
    WebView.loadUrl(filePath);
}

The commented out filePath works as it is calling an external web page. The file.exists() condition does not seem to print the file path in the logs. However, the second one below does. It looks like according to AS, the file does not exist. But i can see it in my folder with is under src/main/assets.

There seems to be many questions asked about this like what can be found here They seem to demonstrate a working concept and what i have above appears to match the prevailing method for rending an HTML file in Android.

What am i doing wrong?

Thank you in advance.

Was it helpful?

Solution

Try:

String filePath = "file:///android_asset/www/index.html";

Instead of:

String filePath = "file///android_asset/www/index.html";

Looks like you forgot the dots : after file

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top