Sample code for 30+ languages & platforms
Android™

Convert Inline Data-URL Images to Related Parts

See more Email Object Examples

Demonstrates the Chilkat Email.ConvertInlineImages method, which converts images embedded inline within HTML (as base64 data URLs) into multipart/related MIME parts referenced from the HTML by cid:. This example sets an HTML body containing a data-URL image and converts it.

Background: HTML can embed an image two ways: as a data: URL (the base64 bytes sit right in the src attribute) or as a related part referenced by cid:. Many email clients don't render data: URLs for security reasons, so converting them to proper multipart/related parts — the traditional email way to embed images — greatly improves how reliably the images display.

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;

    //  Demonstrates the ConvertInlineImages method, which converts images embedded inline within
    //  HTML (as base64 data URLs) into multipart/related MIME parts referenced from the HTML by
    //  CID.

    CkEmail email = new CkEmail();
    email.put_Subject("Convert inline images");

    //  An HTML body with a base64 data-URL image (a 1x1 PNG).
    email.SetHtmlBody("<html><body><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==\"/></body></html>");

    Log.i(TAG, "NumRelatedItems before = " + String.valueOf(email.get_NumRelatedItems()));

    //  Convert the inline data-URL image(s) to multipart/related parts referenced by CID.

    success = email.ConvertInlineImages();
    if (success == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    Log.i(TAG, "NumRelatedItems after = " + String.valueOf(email.get_NumRelatedItems()));

  }

  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."
  }
}