Android™
Android™
Get an Attachment's Bytes into a BinData
See more Email Object Examples
Demonstrates the Chilkat Email.GetAttachmentBd method, which copies an attachment's binary data into a BinData object. The first attachment is at index 0. This example adds an attachment and copies its bytes into a BinData, printing the byte count.
Background: This is the safe, binary way to extract an attachment — the counterpart to the text-oriented
GetAttachmentString. Because attachments are often binary (PDFs, images, archives), copying the raw bytes into a BinData preserves them exactly, ready to write to a file, hash, or pass to another API without any charset conversion that could corrupt the data.Chilkat Android™ Downloads
// 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 GetAttachmentBd method, which copies an attachment's binary data into a
// BinData object. The first attachment is at index 0.
CkEmail email = new CkEmail();
email.put_Subject("GetAttachmentBd example");
email.AddStringAttachment("notes.txt","Some notes stored in the attachment.");
// Copy the first attachment's binary data into a BinData object.
CkBinData bd = new CkBinData();
success = email.GetAttachmentBd(0,bd);
if (success == false) {
Log.i(TAG, email.lastErrorText());
return true;
}
Log.i(TAG, "Attachment size (bytes) = " + String.valueOf(bd.get_NumBytes()));
}
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."
}
}