Send a file to a contact

Before you begin: Make sure that you have completed the task, Register your application with the BlackBerry Messenger platform, and that the class that displays the MyBBMScreen screen in the following code sample passes in a reference to the application's associated BBMPlatformContext object.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.bbm.platform.*;
    import net.rim.blackberry.api.bbm.platform.service.*;
  2. Create a class that extends MainScreen. In the constructor for this screen class, pass in the BBMPlatformContext that is associated with your application.
    public class MyBBMScreen extends MainScreen
    {
        public MyBBMScreen(BBMPlatformContext platformContext) 
        {
    
        }
    }
  3. In the constructor, invoke UIService.showContactPicker() to display a contact picker that allows the user to choose the contact to send the file too. The following code sample displays a contact picker with all the user's BlackBerry Messenger contacts. The selected set of contacts is returned as an enumeration.
    public class MyBBMScreen extends MainScreen
    {
        public MyBBMScreen(BBMPlatformContext platformContext) 
        {
            BBMPlatformContactList contacts = plstformContext.getUIService().showContactPicker("Send File To", ContactListProvider.BBM_CONTACTS_WITH_APP, false, false);
        }
    }
  4. If the user chose a contact from the contact picker, retrieve a reference to first element in the enumeration.
    public class MyBBMScreen extends MainScreen
    {
        public MyBBMScreen(BBMPlatformContext platformContext) 
        {
            BBMPlatformContactList contacts = plstformContext.getUIService().showContactPicker("Send File To", ContactListProvider.BBM_CONTACTS_WITH_APP, false, false);
        
            if (contacts.size() > 0)
            {
                BBMPlatformContact contact = (BBMPlatformContact)contacts.getAll().nextElement();
            }
        }
    }
  5. Invoke FilePicker.getInstance() to display a file picker that allows the user to choose the file that they want to send. When the user has selected a file, assign the file path to a String.
    public class MyBBMScreen extends MainScreen
    {
        public MyBBMScreen(BBMPlatformContext platformContext) 
        {
            BBMPlatformContactList contacts = plstformContext.getUIService().showContactPicker("Send File To", ContactListProvider.BBM_CONTACTS_WITH_APP, false, false);
        
            if (contacts.size() > 0)
            {
                BBMPlatformContact contact = (BBMPlatformContact)contacts.getAll().nextElement();
       
                FilePicker filePicker = FilePicker.getInstance();
                String filePath = filePicker.show();
            }
        }
    }
  6. Invoke MessagingService.sendFile() to send the file.
    public class MyBBMScreen extends MainScreen
    {
        public MyBBMScreen(BBMPlatformContext platformContext) 
        {
            BBMPlatformContactList contacts = plstformContext.getUIService().showContactPicker("Send File To", ContactListProvider.BBM_CONTACTS_WITH_APP, false, false);
        
            if (contacts.size() > 0)
            {
                BBMPlatformContact contact = (BBMPlatformContact)contacts.getAll().nextElement();
       
                FilePicker filePicker = FilePicker.getInstance();
                String filePath = filePicker.show();
    
                platformContext.getMessagingService().sendFile(contact, filePath, "Here's the file");
            }
        }
    }

Code sample: Sending a file to a contact

The following code sample assumes that the class that pushed the MyBBMScreen has access to the BBMPlatformContext object for this application and has passed it into the MyBBMScreen constructor.

import net.rim.blackberry.api.bbm.platform.*;
import net.rim.blackberry.api.bbm.platform.service.*;

public class MyBBMScreen extends MainScreen
{
    public MyBBMScreen(BBMPlatformContext platformContext) 
    {
        BBMPlatformContactList contacts = platformContext.getUIService().showContactPicker("Send File To", ContactListProvider.BBM_CONTACTS_WITH_APP, false, false);
    
        if (contacts.size() > 0)
        {
            BBMPlatformContact contact = (BBMPlatformContact)contacts.getAll().nextElement();
   
            FilePicker filePicker = FilePicker.getInstance();
            String filePath = filePicker.show();

            platformContext.getMessagingService().sendFile(contact, filePath, "Here's the file");
        }
    }
}
Back To Top

Was this information helpful? Send us your comments.