title: How to save webpage as web archive and then display the web archive as a webpage using Android's WebView?
body: <p>I am developing an Android application in Android Studio 3.0.1, Xubuntu 16.04 LTS, using a WebView as a wrapper for a responsive web app that contains HTML, CSS, JavaScript/jQuery and uses PHP/MySQL for the backend. Almost everything is working as expected: the pages fully render, JavaScript runs and the sign-up/login/news-feed systems work as they should. </p>
  
  <p>But now I would like to implement some kind of simple caching system, so that the user can still see the last available version of the pages when there's no Internet connection.</p>
  
  <p>As of now, I've managed to detect whether there is Internet connection; In case there's no connection, the WebView displays a local "Error" page, stored in the app files - which is better than the default Android "Connection Error" screen, but still the user can't see the images and text posts from the news feed when offline like in the Facebook app.</p>
  
  <p>What I have tried (Java/Android Studio):</p>
  
  <p><strong>When the user opens the app (onCreate)</strong>:</p>
  
  <ul>
  <li><p>If there's no connection, check for a locally saved version of the page it's trying to retrieve; If there is connection, save the page as a .mht file for the next time:</p>
  
  <pre><code>if (!isConnected()) {
  
      try {
  
          File file = new File(getFilesDir(), FilenameUtils.getBaseName(url));
          webView.loadUrl("file://" + file.getAbsolutePath() + ".mht");
  
      } catch (Exception e) {
  
          Toast.makeText(getApplicationContext(), "Exception detected", Toast.LENGTH_LONG).show();
  
      }
  
  } else {
  
      try {
  
          File file = new File(getFilesDir(), FilenameUtils.getBaseName(url));
          webView.loadUrl(url);
          webView.saveWebArchive("file://" + file.getAbsolutePath() + ".mht");
  
      } catch (Exception e) {
  
          Toast.makeText(getApplicationContext(), "Exception detected", Toast.LENGTH_LONG).show();
  
      }
  
  }
  </code></pre></li>
  </ul>
  
  <p>I have tried using <code>webView.saveWebArchive()</code> with different paths, including simple <code>/path/to/file</code>, <code>file:///path/to/file</code>, etc., to no avail. The closest I got to displaying something on the WebView was a bunch of raw MHTML, but apparently it wasn't rendered. I also got "File not found" screens and plain white screens without anything on them.</p>
  
  <p>How can I properly save a web page from a WebView into a web archive, and then retrieve from the local storage and display it as a web page again?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>As suggested by greenapps in the comments, I overrode the "onPageFinished" method in the WebViewClient and called "saveWebArchive()" there:</p>
  
  <pre><code>@Override
  public void onPageFinished(WebView view, String link) {
  
      view.saveWebArchive(path);
      showMsg("Finished loading");
  
  }
  </code></pre>
  
  <p>When the phone is connected to the Internet and the home page loads, it does show "Finished loading". After closing the app, disconnecting and opening it again, it now shows a blank background with the color of the original page, no more white pages or error messages. But there aren't any images, text or anything.</p>