2

I am creating a app and using WebView for open URL. I see some apps offer "Save Page"(A web page) option to user.

I want to know how to Save a page from my WebView so that I can show it to user when it request.

Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105

1 Answers1

2

Maybe using a cache is the best way... for that you should check http://developer.android.com/reference/android/webkit/WebSettings.html

"Manages settings state for a WebView. When a WebView is first created, it obtains a set of default settings. These default settings will be returned from any getter call. A WebSettings object obtained from WebView.getSettings() is tied to the life of the WebView. If a WebView has been destroyed, any method call on WebSettings will throw an IllegalStateException."

Especifically:

public static final int **LOAD_CACHE_ONLY**

Since: API Level 1 Don't use the network, load from cache only. Use with setCacheMode(int). Constant Value: 3 (0x00000003)

Or

public static final int **LOAD_CACHE_ELSE_NETWORK**

Since: API Level 1 Use cache if content is there, even if expired (eg, history nav) If it is not in the cache, load from network. Use with setCacheMode(int). Constant Value: 1 (0x00000001)

Update1:

hmm "crappy code of all life" for example:

public static InputStream fetch(String url) throws MalformedURLException,
        IOException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    HttpResponse response = httpClient.execute(request);
    return response.getEntity().getContent();
}


private static String convertStreamToString(InputStream is)
        throws IOException {

    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer, "UTF-8");
    return writer.toString();
}

So, you can fetch a url with InputStream fetch(String url) an then convert that stream to String with private static String convertStreamToString(InputStream is) and save that stream to a file, later you can load that content to a webview..... to read later

Update2:

Or you can do some Java Serialization stuff... cant remember if this work on android xD

Discover the secrets of the Java Serialization API http://java.sun.com/developer/technicalArticles/Programming/serialization/

yeradis
  • 5,235
  • 5
  • 25
  • 26
  • But this is the WebView cache. This will not act like We do on PC. Where we save the page on SDCard. Like "WikiDroid" offer "Save Page" and save it to SDcard. – Arslan Anwar Sep 29 '11 at 14:02
  • If you want to save the page on sd card then you can fetch the url text and write it to sdcard – yeradis Sep 29 '11 at 15:05
  • Writing contents to file on SDCard Looking is very good Idea. Let me try... Thanks – Arslan Anwar Sep 30 '11 at 05:36
  • be careful on doing that, because usually when you load html text into a webview and there are tags or external calls your webview will CRACK(will no load those) xDDDD, thats why i suggested you using the cache – yeradis Sep 30 '11 at 08:33