如何获得附件的总大小

时间:2013-05-29 08:24:56

标签: android filesize

我无法获取邮件附件大小。因此无法为progressbar提供正确的参数(通过publishProgress)。任何帮助将不胜感激。

以下是我正在使用的代码:

    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());
    }

1 个答案:

答案 0 :(得分:0)

您可以使用以下方式轻松找到下载的电子邮件文件的大小:

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

相关链接: Get the file size in android sdk?

修改

要在下载前获取文件大小,请检查以下内容

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

Trying to get downloading file size, but get an error

我认为这些链接会对您有所帮助。

希望它有所帮助。