Sample code for 30+ languages & platforms
Android™

Get a Related Item as Text

See more Email Object Examples

Demonstrates the Chilkat Email.GetRelatedString method, which returns the text of a related item (with CR line-endings), interpreting the item's bytes using the supplied charset. It is intended for text-based related items such as style sheets. The first argument is the zero-based related-item index and the second is the charset. This example reads an embedded CSS style sheet as text.

Background: Not every related item is a binary image — some are text, like a CSS style sheet embedded so the HTML renders consistently offline. For those, GetRelatedString decodes the bytes back to characters using the charset you specify (typically utf-8). For binary related items such as images, read the raw data via the BinData-based accessor instead. This variant normalizes line endings to bare CR; use GetRelatedStringCrLf when you need CRLF.

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);

    //  Demonstrates the GetRelatedString method, which returns the text of a related item (with
    //  CR line-endings), interpreting the bytes using the specified charset.  This is intended
    //  for text-based related items such as style sheets.  The index is zero-based.

    CkEmail email = new CkEmail();
    email.put_Subject("GetRelatedString example");

    //  The HTML references a related style sheet by name (Content-Location).
    email.SetHtmlBody("<html><head><link rel=\"stylesheet\" href=\"styles.css\"/></head><body>Styled.</body></html>");

    //  Add the related style sheet (index 0).
    email.AddRelatedString2("styles.css","body { color: navy; }","utf-8");

    //  Get the first related item (index 0) as text, interpreting the bytes as utf-8.
    String content = email.getRelatedString(0,"utf-8");
    Log.i(TAG, "Related item 0 text: " + content);

  }

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