I am having an android application requirement where i need to open saved web pages, how to do the same?? FIrst of all, how can we save a webpage with its dependancies on android and later open it in your applications? Any inputs will be of great help!
            Asked
            
        
        
            Active
            
        
            Viewed 5,434 times
        
    2 Answers
4
            
            
        First of all, let's save the webarchive from webview
private void initUI{
     webView = (WebView) findViewById(R.id.webView);
     AndroidWebClient client = new AndroidWebClient();
     webView.setWebViewClient(client);
     WebSettings webSettings = webView.getSettings();
     webSettings.setJavaScriptEnabled(true);
}
private class AndroidWebClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url,
                                  android.graphics.Bitmap favicon) {                
        }
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            view.saveWebArchive(Environment.getExternalStorageDirectory()
                    + File.separator +"myArchive"+".mht");
            // our webarchive wull be available now at the above provided location with name "myArchive"+".mht"
        }
        public void onLoadResource(WebView view, String url) {
        }
      }
The way to save the webarchive is same in all APIs but to load it, varies
for API less than Kitkat
private void loadArchive(){
     String rawData = null;
        try {
            rawData =   getStringFromFile(Environment.getExternalStorageDirectory()
                    + File.separator+"myArchive"+".mht");
        } catch (Exception e) {
            e.printStackTrace();
        }
webView.loadDataWithBaseURL(null, rawData, "application/x-webarchive-xml", "UTF-8", null);
}
  public String getStringFromFile (String filePath) throws Exception {
        File fl = new File(filePath);
        FileInputStream fin = new FileInputStream(fl);
        String ret = convertStreamToString(fin);
        //Make sure you close all streams.
        fin.close();
        return ret;
    }
    public  String convertStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        reader.close();
        return sb.toString();
    }
for Kitkat and above
private void loadArchive(){
   webView.loadUrl("file:///"+Environment.getExternalStorageDirectory()
                + File.separator+"myArchive"+".mht");
}
 
    
    
        DeltaCap019
        
- 6,532
- 3
- 48
- 70
- 
                    Hi.. I am following the same steps for saving and loading the web page in .mht format. But when I load the .mht file on the webView, the webView appears blank. What can be the issue for that ? – thedarkpassenger Apr 10 '17 at 09:58
2
            
            
        This is how I would implement that:
- Save original web page into a file
- Parse saved file and get all image URLs. Save images into the same directory.
- Convert URL of the images(bind all links to a local copies)
Here is simple code which demonstrate this idea:
String download(String url) throws Exception {
    String filename = "local.html";
    save(url, filename);
    List<String> imageLinks = getImageURLs(filename);
    for (String imageLink : imageLinks) {
        String imageFileName = getImageName(imageLink);
        save(imageLink, imageFileName);
    }
    convertImageURLs(filename);
    return filename;
}
void save(String url, String saveTo) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) (new URL(url)).openConnection();
    conn.connect();
    InputStream is = conn.getInputStream();
    save(is, saveTo);
}
void save(InputStream is, String saveTo) {
    // save actual content
}
List<String> getImageURLs(String localHtmlFile) {
    // parse localHtmlFile and get all URLs for the images
    return Collections.EMPTY_LIST;
}
String getImageName(String imageLink) {
    // get image name, from url
    return null;
}
void convertImageURLs(String localHtmlFile) {
    // convert all URLs of the images, something like:
    // <img src="original_url"> -> <img src="local_url">
}
 
    
    
        Eugene
        
- 520
- 3
- 8
- 
                    Hi, thanks for the reply, this seems to be very complicated, is there no easy way to do it as we do in desktop? – bharath Jul 08 '13 at 08:44
- 
                    1Take a look at [this post](http://stackoverflow.com/questions/14670638/webview-load-website-when-online-load-local-file-when-offline), seems that's what you are looking for. – Eugene Jul 08 '13 at 10:57
