2

I am having trouble in getting the mail attachment size. So not able to provide proper argument to progressbar (via publishProgress). Any help will be appreciated.

Below is the code I am using:

    try{
            store = s.getStore("imaps");
            store.connect("imap.gmail.com", "myemail@gmail.com", "password");
            Folder inbox = store.getFolder("Inbox"); //Shantivan Rosary
            inbox.open(Folder.READ_ONLY);
            msgs = inbox.getMessages();
            m=(Multipart)msgs[inbox.getMessageCount()-1].getContent(); //Getting the newest email. Assuming it has only one attachment. You can extend
            for(int i=0;i<m.getCount();i++){
                    bp = m.getBodyPart(i);
                    disposition = bp.getDisposition();
                    if(disposition!=null && (disposition.equals("ATTACHMENT"))){
                            fileName = bp.getFileName();
                            base64dec = (InputStream)bp.getContent();
                            OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+fileName);
                            byte data[] = new byte[8192];
                            long total = 0;
                            while ((count = base64dec.read(data)) != -1){
                                    total += count;
                                    output.write(data, 0, count);
                                    publishProgress((int)total);
                            }
                            output.flush();
                            output.close();base64dec.close();
                    }
            }
    }catch(Exception e){
            errorMsg += "\nError: "+e.toString();
            Log.e("MyError:",e.toString());
    }
ravi tiwari
  • 521
  • 1
  • 8
  • 24

1 Answers1

0

You can easily find the size of the downloaded email file in bytes using :

File file=new File(Environment.getExternalStorageDirectory(), "example.txt");
long length=file.length();//returns size in bytes 

Related link: Get the file size in android sdk?

EDIT

For getting file size before downloading ,check the following

How to know the size of a file before downloading it?

Trying to get downloading file size, but get an error

I think these links will help you.

Hope it helps .

Community
  • 1
  • 1
Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • No Rachita... the file is getting generated and I am publishing the progress in Progressbar so I need the filesize in advance; not after creating it. – ravi tiwari May 29 '13 at 08:47
  • I used bp.getSize() to get the size. But this is not giving correct answer. The actual file size is 2097152 while bp.getSize() is giving me 2883928. And this is causing progressbar to dismiss at around 74%. Any idea what is wrong/missing? – ravi tiwari May 29 '13 at 08:55
  • sorry for misunderstanding your question .I have edited my ans and added links for getting download size. Pls check them. – Rachita Nanda May 29 '13 at 09:50
  • If you will see my code closely you will find that I can't use getContentLength(); as I am not getting connected using http:// connection. Any other way to get the file size in the code I am using? – ravi tiwari May 30 '13 at 01:03