Download attachments automatically

When a message arrives in the messages application on the BlackBerry® device with an attachment, you can automatically download the attachment and store it on the BlackBerry device.

Before downloading attachments, the AttachmentDownloadManager class validates the attachment. AttachmentDownloadManager throws exceptions if any of the following conditions occur.

  • the application attempts to invoke the download while a download is already in progress
  • the application attempts to download zero length files.
  • the size of the attachment is larder than permitted by the application IT policy or service books
  • attachments are encrypted
  • there is not enough space available on the BlackBerry device or SD card

The AttachmentDownloadManager.download() method performs verification during the download process. If verification errors are found, the method throws an exception. For a list of possible verification errors, see the API reference for the BlackBerry® Java® Development Environment.

Note: The BlackBerry® Attachment Service receives all attachments first. Third-party attachment handlers cannot override this default behavior. For more information about the BlackBerry Attachment Service, see the BlackBerry Enterprise Server Administration Guide.
  1. Import the required classes and interfaces.
    import java.io.IOException;
    import net.rim.blackberry.api.mail.*;
    
  2. Implement the DownloadProgressListener interface. Create an instance of the AttachmentDownloadManager class.
    public class AutoAttachTest implements DownloadProgressListener 
    {
       AttachmentDownloadManager _adm = new AttachmentDownloadManager();
  3. Use the methods available in AttachmentDownloadManager to determine information about the attachment.
    public String fileSize = getFileSize(BodyPart bodyPart);
    public String fileName = getFileName(BodyPart bodyPart);
    public String fileType = getFileContentType(BodyPart bodyPart);
    public String filePath = getDownloadedFileName(BodyPart bodyPart);
  4. Invoke AttachmentDownloadManager.download(), to download the attachment.
    _adm.download(bodyParts, null, this);
  5. Override the DownloadProgressListener callback methods, to provide updates about the status of the download of the attachment.
    private void downloadCancelled(String msg)
    {
       BodyPart bodyPart = (BodyPart) element;
       _screen.displayProgress("Failed to download " + _adm.getFileName(bodyPart));
    }
    private void downloadCompleted(Object element)
    {
       BodyPart bodyPart = (BodyPart) element;
       _screen.displayProgress(_adm.getFileName(bodyPart) + " downloaded.");
    }
    public void updateProgress(Object element, int current, int total)
    {
    }
Next topic: Calendar

Was this information helpful? Send us your comments.