Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 1
Registered: ‎01-07-2009
My Device: Not Specified

Sending an image as an attachment

Hey everyone,

I'm new to blackberry development. I've gone through numerous posts on the same subject and I'm still a little confused. From what I understand, the ESS on the blackberry simulator has some limitations which prevents us from sending large attachment files (over 4 kb). Has anyone figured out yet how to get around this problem?

 

Also, I've been facing another problem. I tried attaching a small PNG image about 2kb in size. When I tried sending the image as an attachment, in the ESS I got an error saying content type 5 not implemented. However, the mail was sent successfully without the attachment. I've attached my code below, please let me know what's wrong.

 

 

public class EmailSample extends MainScreen {
            private AutoTextEditField subjectField, msgField;
            private EditField toField;
            private byte[] rawone;
            private Bitmap _createphoto=Bitmap.getBitmapResource("auto.png");

            public EmailSample(byte[] raw) {
                super();
                LabelField title = new LabelField("Send Image",
                                LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
                setTitle(title);
                toField = new EditField("To: ", "", Integer.MAX_VALUE, BasicEditField.FILTER_EMAIL);
                subjectField = new AutoTextEditField("Subject: ", "New Mail", 30, BasicEditField.FILTER_DEFAULT);
                //ignore this
                rawone = raw;
                //_createphoto = Bitmap.createBitmapFromBytes(rawone, 0, -1, 7);
                //BitmapField photo = new BitmapField(_createphoto);
                add(toField);
                add(subjectField);
                add(new SeparatorField());
                //add(msgField);
                //add(photo);
            }

            //create a Send menu item to send the email message
            private MenuItem _sendItem = new MenuItem("Send", 110, 10) {
                public void run() {
                    DataBuffer db = new DataBuffer();
                    db.setBigEndian(false);
                    int width=_createphoto.getWidth();
                    int height=_createphoto.getHeight();
                    int[] rgbdata = new int[width*height];
                   
                    byte[] ba;
                    _createphoto.getARGB(rgbdata, 0, width, 0, 0, width, height);
                     for (int i = 0; i < (width * height); i++)
                     {
                        db.writeInt(rgbdata[i]);
                     }

                    db.rewind();


                    ba = db.getArray();
                    Multipart mp = new Multipart();
                   
                    //data for the content of the file
                    String messageData = "Sending Photo";
                    SupportedAttachmentPart sap = new SupportedAttachmentPart(mp,"image/bmp" ,"image.bmp",ba );    
                    TextBodyPart tbp = new TextBodyPart(mp,messageData);
                    mp.addBodyPart(sap);

                    //create a message in the sent items folder
                    Folder folders[] = Session.getDefaultInstance().getStore().list(Folder.SENT);

                    Message msg = new Message(folders[0]);
                    Address toList[] = new Address[1];
                    try {
                         toList[0]= new Address(toField.getText(), "");
                    } catch(AddressException e) {
                        System.out.println(e.toString());
                    }
                   
                    try {
                        msg.addRecipients(Message.RecipientType.TO, toList);
                        msg.setSubject(subjectField.getText());
                        msg.setContent(mp);
                    } catch(MessagingException e) {
                         //handle exception
                    }

                     //send the message using the static Transport.send method
                     //then reset UI fields to be empty
                     try {
                         Transport.send(msg);
                         toField.setText("");
                         subjectField.setText("");
                        
                     } catch(MessagingException e) {
                         System.out.println(e.getMessage());
                     }
                }
           };

           //override makeMenu to add the Send and Close menu items to the menu
           protected void makeMenu( Menu menu, int instance )
           {
               menu.add(_sendItem);
           }
     }

Thanking you in advance.

Highlighted
BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: Sending an image as an attachment

You can work around the ESS limitations by testing on a real BlackBerry handheld or by connecting the BlackBerry Simulator to a BlackBerry Enterprise Server.  The link below has steps on how to do so.

 

How To - Connect the BlackBerry Device Simulator to a BlackBerry Enterprise Server
Article Number: DB-00049

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800792/801083/How_To_...

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker