Sample code for 30+ languages & platforms
Android™

Set a MIME Body from Binary Bytes (BinData)

See more MIME Examples

Demonstrates the Chilkat Mime.SetBodyBd method, which sets the decoded body bytes from a BinData, changes the transfer encoding to base64, and preserves the bytes exactly. The only argument is the BinData.

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 direct way to put arbitrary binary content into a part from memory — a generated image, a decrypted blob — without a temporary file. Chilkat base64-encodes it so the bytes survive text-based transports unchanged. Note it does not set a Content-Type or disposition, so assign those yourself to describe what the bytes are.

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.SetBodyBd method, which sets the decoded body bytes from a BinData,
    //  changes the transfer encoding to base64, and preserves the bytes exactly.  The only argument is
    //  the BinData.

    //  Put the binary body bytes into a BinData.
    CkBinData bd = new CkBinData();
    success = bd.LoadFile("qa_data/photo.jpg");
    if (success == false) {
        Log.i(TAG, bd.lastErrorText());
        return;
        }

    CkMime mime = new CkMime();

    //  Set a Content-Type for the binary content (SetBodyBd does not set one itself).
    mime.put_ContentType("image/jpeg");

    success = mime.SetBodyBd(bd);
    if (success == false) {
        Log.i(TAG, mime.lastErrorText());
        return;
        }

    Log.i(TAG, "Body set from " + String.valueOf(bd.get_NumBytes()) + " bytes.");

  }

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