Sample code for 30+ languages & platforms
Android™

Set a MIME Body from a File

See more MIME Examples

Demonstrates the Chilkat Mime.SetBodyFromFile method, which reads a local file and sets it as the body, choosing content headers from the filename extension. The only argument is the file path.

Note: 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: This is the one-call way to turn a file into a MIME part: Chilkat reads the bytes and infers the Content-Type and transfer encoding from the extension — .jpg becomes image/jpeg with base64, .txt a text type, and so on. It is the natural building block for adding an attachment, usually followed by setting a Content-Disposition and filename.

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 Mime.SetBodyFromFile method, which reads a local file and sets it as the body,
    //  choosing content headers based on the filename extension.  The only argument is the file path.

    CkMime mime = new CkMime();

    //  The Content-Type and encoding are set from the ".jpg" extension (image/jpeg, base64).
    success = mime.SetBodyFromFile("qa_data/photo.jpg");
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    Log.i(TAG, "Content-Type: " + mime.contentType());
    Log.i(TAG, "Encoding: " + mime.encoding());

  }

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