1

I have already created the app, but it needs to be connected to the internet to work. In essence it just displays the website through an app. I would like to have the website installed on the app so that it can load without requiring an internet connection.

I have downloaded my webpage while, and added it to the Android SDK. Its saved it under the Java section. Now the code I used to load the webpage while online was the following:

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://kuglerdesign.com/");
        mWebView.setWebViewClient(new MyAppWebViewClient());

I have searched on stackoverflow for a similar question, and have found the following lines of code:

URL url = this.getClass().getResource("/com/path/to/file.txt")

File file = new File(url.toURI());

and these from a different answer:

File currFile = new File(getClass().getClassLoader().getResource("the_file.txt").getFile());

My question is how to implement these to a file thats in the Android SDK. Do I just replace all the mWebView and WebSettings or do I still need them as its a HTML file?

Or would I be better off just to set the mWebView Url to a local file? So replacing the following line of code:

mWebView.loadUrl("http://kuglerdesign.com/");

with this line :

mWebView.loadUrl("file:///kuglerdesign.html");

Any help would be greatly appreciated.

(sorry for being such a novice at Java :( ...)

  • You can find your answer here : http://stackoverflow.com/questions/4027701/loading-existing-html-file-with-android-webview – Guillaume Aug 03 '15 at 15:15
  • possible duplicate of [Load local HTML file into WebView](http://stackoverflow.com/questions/5749569/load-local-html-file-into-webview) – msal Aug 03 '15 at 15:39

1 Answers1

0

You have to put your html-files into the assets directory.

d.android.com states for the assets folder:

You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.

The WebView can directly load your files, by using file:///android_asset/ as URI path, which can be read here.

That would make your call as easy as mWebView.loadUrl("file:///android_asset/kuglerdesign.html");

msal
  • 947
  • 1
  • 9
  • 30
  • Tried it now, it works. Thank you so very much! Many answers talked about the "file:///" link, but none linked it with the assets folder. Thank you again, this completely solved my problem. – MaddenedMage Aug 04 '15 at 08:37