saveWebArchive() Method - Saving Web Archive Files

This section provides a tutorial example on how to use the saveWebArchive() method to save Web page content in WebView to Web archive files in the application's files folder.

Now let me make the WebViewClient subclass more useful. In the onPageFinished() callback method, I want to save the Web page as a Web archive file using the saveWebArchive() method. Here is my AndroidWeb version 3.1:

 * Version 3.1 - Saving content as Web archive file
 * Copyright (c) 2015,, All Rights Reserved.
package com.herongyang.web;

import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Context;
import java.util.*;
public class AndroidWeb extends Activity {
   public void onCreate(Bundle savedInstanceState) {

      WebView view = new WebView(this);
      AndroidWebClient client = new AndroidWebClient();
   private class AndroidWebClient extends WebViewClient {
      public void onPageStarted(WebView view, String url,  favicon) {
         logLine(view, "onPageStarted() called: url = "+url);
      public void onPageFinished(WebView view, String url) {
         logLine(view, "onPageFinished() called: url = "+url);
         // view.saveWebArchive("Google.xml"); // Incorrect use
            + File.separator + System.currentTimeMillis()+".xml");
      public void onLoadResource(WebView view, String url) {
         logLine(view, "onLoadResource() called: url = "+url);
      public void logLine(WebView view, String msg) {
         try {
            FileOutputStream fos = 
               .openFileOutput("Activity.log", Context.MODE_APPEND);
            OutputStreamWriter out = new OutputStreamWriter(fos);
            out.write((new Date()).toString()+": "+msg+"\n");
         } catch (Exception e) {

Do you see the commented out line of view.saveWebArchive("Google.xml")? This will produce the following error in the main log buffer, because saveWebArchive() is expecting a full path name for the output file:

D/webcoreglue( 1301): saveWebArchive: Failed to initialize xml writer.

Rebuild the debug package, reinstall it to the emulator and run it. Google search home page is displayed again.

Enter "herongyang", search and select my Web site. Some Web archive files should be saved in the application "files" folder now.

C:\herong>\local\android-sdk-windows\platform-tools\adb shell
# ls -l /data/data/com.herongyang.web/files
ls -l /data/data/com.herongyang.web/files
-rw------- u0_a73   u0_a73   95072 20:26 1435501582525.xml
-rw------- u0_a73   u0_a73   62359 20:26 1435501590670.xml
-rw------- u0_a73   u0_a73   59273 20:26 1435501592254.xml
-rw------- u0_a73   u0_a73   14874 20:26 1435501596675.xml
-rw-rw---- u0_a73   u0_a73    2658 20:26 Activity.log

AndroidWeb version 3.1 is working! See next tutorial on how to open the Web archive .xml files.

Last update: 2015.

Table of Contents

 About This Book

 Installing JDK 1.8 on Windows System

 Installation of Android SDK R24 and Emulator

 Installing Apache Ant 1.9 on Windows System

 Developing First Android Application - HelloAndroid

 Android Application Package (APK) Files

 Android Debug Bridge (adb) Tool

 Android File Systems

 Android 4.0.3 File Systems

 AboutAndroid - Application to Retrieve System Information Class and Activity Lifecycle

 View Objects and Layout Resource Files

 Using "adb logcat" Command for Debugging

 Build Process and Package File Content

Building Your Own Web Browser

 android.webkit.WebView - Web Browser Base Class

 AndroidWeb - My Own Web Browser

 WebViewClient Subclass - Content Rendering Callbacks

saveWebArchive() Method - Saving Web Archive Files

 Web Archive File - XML File of Base64 Encoded Data

 Android Command Line Shell

 Samsung Galaxy Tab 3 Mini Tablet

 USB Debugging Applications on Samsung Tablet

 Android Tablet - LG-V905R

 USB Debugging Applications on LG-V905R Tablet

 Android Phone - LG-P925g

 USB Debugging Applications on LG-P925g Phone

 Outdated Tutorials


 PDF Printing Version