我在获取邮件附件大小时遇到问题。因此无法为进度条提供正确的参数(通过 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());
}