BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 08-10-2008, 08:43 PM   #1 (permalink)
arv
Knows Where the Search Button Is
 
Join Date: May 2007
Location: indonesia
Model: 8800
PIN: N/A
Carrier: vodafone
Posts: 21
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default How To Programmatically receive Attachment

Please Login to Remove!

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 ..

Last edited by arv : 08-10-2008 at 09:06 PM.
Offline  
Old 08-11-2008, 03:59 AM   #2 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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).
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 08-11-2008, 04:18 AM   #3 (permalink)
arv
Knows Where the Search Button Is
 
Join Date: May 2007
Location: indonesia
Model: 8800
PIN: N/A
Carrier: vodafone
Posts: 21
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by simon.hain View Post
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.
Offline  
Old 08-11-2008, 05:45 AM   #4 (permalink)
CrackBerry Addict
 
Join Date: Apr 2005
Location: hamburg, germany
Model: 8900
Carrier: o2
Posts: 838
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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().
__________________
java developer, Devinto, hamburg/germany
Offline  
Old 08-11-2008, 11:05 AM   #5 (permalink)
arv
Knows Where the Search Button Is
 
Join Date: May 2007
Location: indonesia
Model: 8800
PIN: N/A
Carrier: vodafone
Posts: 21
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by simon.hain View Post
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.
Offline  
Old 08-11-2008, 11:42 AM   #6 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Old 08-11-2008, 11:46 AM   #7 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Old 08-11-2008, 12:05 PM   #8 (permalink)
arv
Knows Where the Search Button Is
 
Join Date: May 2007
Location: indonesia
Model: 8800
PIN: N/A
Carrier: vodafone
Posts: 21
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by holy3daps View Post
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.

Last edited by arv : 08-11-2008 at 12:14 PM.
Offline  
Old 08-11-2008, 03:56 PM   #9 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Old 08-13-2008, 05:29 AM   #10 (permalink)
arv
Knows Where the Search Button Is
 
Join Date: May 2007
Location: indonesia
Model: 8800
PIN: N/A
Carrier: vodafone
Posts: 21
Post Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb 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.
Offline  
Old 08-13-2008, 09:01 AM   #11 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2006
Location: Boston
Model: 8900
Carrier: AT&T
Posts: 98
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Cheers,

karl
__________________
Karl G. Kowalski
---------------
Owns a RAZR
Develops for BlackBerry
So next phone will be........an iPhone 3G!
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.