Sample code for 30+ languages & platforms
Android™

Add a Related Item from a BinData Object

See more Email Object Examples

Demonstrates the Chilkat Email.AddRelatedBd method, which adds a related item (such as an inline image) using the contents of a BinData object, and returns the generated Content-ID. This example loads an image into a BinData, adds it, captures the Content-ID, and references it from the HTML body.

Background: This is the binary, Content-ID-based way to embed an inline resource — the right choice for images, whose bytes belong in a BinData rather than a string. Because Chilkat generates the Content-ID, the usual pattern is: add the item first, capture the returned ID, then build the matching <img src="cid:..."> reference from it (done here with a StringBuilder).

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 AddRelatedBd method, which adds a related item (such as an inline image)
    //  using the contents of a BinData object, and returns the generated Content-ID.

    CkEmail email = new CkEmail();
    email.put_Subject("Related image from BinData");

    //  Load the image data from a file into a BinData object.
    CkBinData bdImage = new CkBinData();
    success = bdImage.LoadFile("qa_data/images/logo.png");
    if (success == false) {
        Log.i(TAG, bdImage.lastErrorText());
        return true;
        }

    //  Add the image as a related item; capture its generated Content-ID.
    String cid = email.addRelatedBd("logo.png",bdImage);
    if (email.get_LastMethodSuccess() == false) {
        Log.i(TAG, email.lastErrorText());
        return true;
        }

    //  Reference the related item in the HTML body by its Content-ID.
    CkStringBuilder sbHtml = new CkStringBuilder();
    sbHtml.Append("<html><body><img src=\"cid:PLACEHOLDER_CID\"/></body></html>");
    int numReplaced = sbHtml.Replace("PLACEHOLDER_CID",cid);
    email.SetHtmlBody(sbHtml.getAsString());

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

    //  Note: The path "qa_data/images/logo.png" is a relative local filesystem path,
    //  relative to the current working directory of the running application.

  }

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