Sample code for 30+ languages & platforms
Android™

Unpack MHT MIME Text into Files

See more MHT / HTML Email Examples

Demonstrates Mht.UnpackMHTString, which extracts MHT data into its constituent parts: the HTML document and its referenced resources. The source is MHT MIME text supplied as a string.

The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background. Despite the method name, the source may be either literal MHT MIME text or a local MHT file path; Chilkat distinguishes the two by detecting MIME headers. The HTML is written under the chosen filename, and referenced parts are written into a sub-directory.

Chilkat Android™ Downloads

Android™
// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    boolean success = false;

    CkMht mht = new CkMht();

    //  The file paths are relative to the application's current working directory.  Absolute paths may
    //  also be used.  Supply the paths appropriate to your own environment.

    //  Load the MHT MIME text into a string.
    CkStringBuilder sbMht = new CkStringBuilder();
    boolean bLoaded = sbMht.LoadFile("qa_data/page.mht","utf-8");
    if (bLoaded != true) {
        Log.i(TAG, "Failed to load the MHT file.");
        return;
        }

    //  Unpack the MHT MIME text into its constituent parts.  The 1st argument may be MHT MIME text or a
    //  file path; the 2nd is the unpack directory, the 3rd is the main HTML filename, and the 4th is the
    //  sub-directory (relative to the unpack directory) for the referenced parts such as images and style
    //  sheets.
    success = mht.UnpackMHTString(sbMht.getAsString(),"qa_output/unpacked","index.html","parts");
    if (success == false) {
        Log.i(TAG, mht.lastErrorText());
        return;
        }

    Log.i(TAG, "MHT unpacked.");

  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}