BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   How To Programmatically receive Attachment (http://www.blackberryforums.com/developer-forum/144579-how-programmatically-receive-attachment.html)

arv 08-10-2008 07:43 PM

How To Programmatically receive Attachment
 
Hi All,

where i can find example about getting attachment from a Message (mail API)?

I already implement folderlistener, and its messagesAdded(FolderEvent).
I got Message from FolderEvent, Message as instanceof Multipart.

the Multipart consist of 2 BodyParts,
my attachment in BodyPart index number 1.

I check bodypart.hasMore() it's true, means any content in server.
I call Transport.more(bodyPart, true);
but the bodypart.hasMore() always true (i try to loop about 2 minutes,
but it's always true).
My attachment only about 100 Bytes.

when i check message.getClass().getName() its
belong to UnSupportedAttachmentPart with size == 0.

is it any example how to get attachment by program?

thank you ..

simon.hain 08-11-2008 02:59 AM

you have to implement an attachmenthandler for your content-type first.
you have to prefix your file with x-rimdevice or it will not be delivered to your handheld (you could change some settings on the bes, alternatively).

arv 08-11-2008 03:18 AM

Quote:

Originally Posted by simon.hain (Post 1054752)
you have to implement an attachmenthandler for your content-type first.
you have to prefix your file with x-rimdevice or it will not be delivered to your handheld (you could change some settings on the bes, alternatively).

Thanks simon,
i will try it first.

i will rename my attachment to: x-rimdevice-filename.ext

about the attachment handler, should i register to AttachmentManager
before i can get the attachment (BodyPart index number 1) via Transport.More()?
or register to Attachment as just an option for blackberry to recognize
my attachment as instanceof SupportedAttachmentPart?

i already read your post (in 2005) about retrieve attachment,
now i have same problem, i can't getContent() because it size==0.

thanks.

simon.hain 08-11-2008 04:45 AM

add the attachment handler in an autorun part of your project (see kb article), your second assumption is correct, the system needs to know the filetype to be able to deliver a SupportedAttachmentPart.

Be aware of Attachments not supporting hasMore().

arv 08-11-2008 10:05 AM

Quote:

Originally Posted by simon.hain (Post 1054779)
add the attachment handler in an autorun part of your project (see kb article), your second assumption is correct, the system needs to know the filetype to be able to deliver a SupportedAttachmentPart.

Be aware of Attachments not supporting hasMore().

Simon,
thanks for your attention.

My application is not an autostart app.
I create app that when it executed, it will waiting for FolderEvent,
I already successful while getting Multipart from Event's Message.

i already change my filename to: x-rimdevice-filename.ext
so the attachment will store in handheld (right?).

and its content-type is: application/x-myapp

I add AttachmentHandler in my appMainScreen constructor,
Code:

       
            public appMainScreen() {
            ah = new ahandler();
            //Add this AttachmentHandler to the AttachmentHandlerManager
            ahm = AttachmentHandlerManager.getInstance();
            ahm.addAttachmentHandler(ah);
            ...

this is my inner class:
Code:

        class ahandler implements AttachmentHandler {
            public void run(Message m, SupportedAttachmentPart sap) {
                //Perform required processing on your attachment here.
                //Get the attachment filename.
                System.out.println("Filename: " + sap.getFilename());
                //Get the attachment size.
                System.out.println("Attachment size is: " + sap.getSize());
            }
            public String menuString() {
                return "View ATT";
            }
            public boolean supports(String s) {
                boolean b = false;
                System.err.println("supports:"+s);
                if (s.toLowerCase().indexOf("myapp") != -1) {
                    b = true;
                }
                return b;
            }
        }


when i get BodyPart from Multipart (my attachment on index number 1),
the bodypart's size is 0.
but return true on method bodypart.hasMore().

is it any error on my attachment handler registration?
because when i check into the message,
and press menu on message's attachment, the menu "View ATT"
is not shown.

any other clue?

thanks.

holy3daps 08-11-2008 10:42 AM

Hi!

Some things to think about. As of OS 4.1, registering your attachment handler object with the AttachmentHandlerManager should be a one-time only event. This is because the OS caches the object, and does not release/delete it. So if your application is registering an attachment handler every time your app launches, another one will be added to the list the OS is caching. This is why you should only register your attachment handler with the OS once for every time the device is turned on - this can be arranged through an "Alternate Entry Point" mechanism.

Code:

public void run (Message m, SupportedAttachmentPart sap )
...
String data = new String((byte[])sap.getContent());

The above code turns the contents of the attachment into a String, which can be checked for length. You can avoid the String conversion and just pull the contents out with

Code:

Object contents = sap.getContent();
It should be coming in as a byte array (byte[]) but setting it to an Object makes it easier to perform null checking (for me, anyway). If the Object is non-null, you can convert it to a byte array and then check its length.

Cheers,

karl

holy3daps 08-11-2008 10:46 AM

One more thing: sometimes attachment handlers are unable to respond to attachments in email messages that arrived before the attachment handler was registered. I say "sometimes" because the behavior is somewhat inconsistent (i.e., I used to notice this behavior consistently on OS 4.1 and earlier devices, but I have some tests which have shown that this is not necessarily true on 4.2 or later devices).

Cheers,

karl

arv 08-11-2008 11:05 AM

Quote:

Originally Posted by holy3daps (Post 1055025)
One more thing: sometimes attachment handlers are unable to respond to attachments in email messages that arrived before the attachment handler was registered. I say "sometimes" because the behavior is somewhat inconsistent (i.e., I used to notice this behavior consistently on OS 4.1 and earlier devices, but I have some tests which have shown that this is not necessarily true on 4.2 or later devices).

Cheers,

karl


Halo Karl,
thanks for your information.

about the attachment handler registration,
i addHandler at appMainScreen constructor,
and i removeHandler when user close() the app.

so the handler is one time handler like what you said.


about the sequence of the message,
in this test case, of course i send the message (email),
after i open my application.
so, it should be catch by the handler.


Until now, i still can't do sap.getContent().
I don't know it's caused by error when registering handler
or it's error because of i can't get more() attachment from server.

thanks.

holy3daps 08-11-2008 02:56 PM

Hi!

So, just out of curiosity, would you be willing to try an experiment and use an Alternate Entry Point to attach your attachment handler? I've never tried to do it the way you suggest, and for the application I have developed, this has always worked.

That is, with a few exceptions - Sprint's version of device OS 4.3.X does not work with email attachments very well. Our testing failed every single time, and RIM admitted that Sprint had accepted a version with a bug in the email attachment handling mechanism. Luckily no other service provider is using 4.3.X. However, early releases of OS 4.5 also contain the same bug, and several were accepted by Rogers Wireless and Vodafone Germany. There may be one or two others I've left off the list. So if you're using a 4.3 simulator and/or a 4.3-based device, email attachment handling may not work correctly (I think I was seeing failure in both the simulators and real devices).

Cheers,

karl

arv 08-13-2008 04:29 AM

Problem Solved
 
Thanks all,
problem solved.

then main problem is name of content-type.
don't know why, the blackberry system don't allow
me to use my own name likes "application/x-myapps".
the blackberry will detect as application/octet-stream.

i register attachment handler, and print out every
message that have attachment via EventLogger.

now, my application run well,
attachment handler register at application open,
and attachment handler remove at application close.

thanks.

holy3daps 08-13-2008 08:01 AM

Hey, congratulations and thanks for reporting success! And the details! You're on your way to answering posts now!

Cheers,

karl


All times are GMT -5. The time now is 01:46 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.