Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save webpage for offline use and invoke the same android

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!

like image 516
bharath Avatar asked Jul 05 '13 08:07

bharath


2 Answers

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");
}
like image 102
DeltaCap019 Avatar answered Sep 30 '22 02:09

DeltaCap019


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">
}
like image 45
Eugene Avatar answered Sep 30 '22 00:09

Eugene