下载gmail附件并将其保存到android中的SD卡

时间:2017-01-11 04:49:47

标签: android download gmail attachment

我有所有gmail附件列表,现在我需要下载所选附件,并在用户点击下载按钮后将其保存到SD卡。

我已经使用下面的代码完成了它,但下载需要花费很多时间。

任何人都以更好的方式帮助我。

以下是我的代码。

打开连接并获取所有附件并在listView中显示。

private Message[] emails;
ArrayList<ModelAttachment> attachment = new ArrayList<>();
Store store.connect("pop.gmail.com", "userName", "Password");

            Folder inbox = store.getFolder("Inbox");
            inbox.open(Folder.READ_ONLY);
            Message[] msgs = inbox.getMessages();
            emails = msgs;
            for (int j = 0; j < 100; j++) {
                Message message = msgs[j];
                String contentType = message.getContentType();
                Log.v("j", String.valueOf(j));

                String messageContent = "";
                Multipart multiPart;
                if (contentType.contains("multipart")) {
                    multiPart = (Multipart) message.getContent();
                    for (int i = 0; i < multiPart.getCount(); i++) {
                        MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
                        if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                            // this part is attachment
                            ModelAttachment model = new ModelAttachment();
                            model.setName(part.getFileName().toString());
                            attachment.add(model);

                        } else {
                            // this part may be the message content
                            messageContent = part.getContent().toString();
                        }
                    }
                    if (attachFiles.length() > 1) {
                        attachFiles = attachFiles.substring(0, attachFiles.length() - 2);
                    }

当用户点击代码下面的下载按钮时,将执行。

 for (int k = 0; k < attachment.size(); k++) {
            if (attachment.get(k).isSelected()) {
                String fileName = null;
                try {
                    for (int j = 0; j < emails.length; j++) {
                        Message message = emails[j];
                        String contentType = message.getContentType();
                        if (contentType.contains("multipart")) {
                            Multipart multiPart = (Multipart) message.getContent();
                            for (int i = 0; i < multiPart.getCount(); i++) {
                                MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(i);
                                if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
                                    if (part.getFileName().equals(attachment.get(k).getName()))
                                        part.saveFile("sdCardPath" + File.separator + part.getFileName());
                                }
                            }
                        }
                    }
                } catch (MessagingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

这是模型

public class ModelAttachment {

Object name;
boolean isSelected;

public Object getName() {
    return name;
}

public void setName(Object name) {
    this.name = name;
}

public boolean isSelected() {
    return isSelected;
}

public void setSelected(boolean selected) {
    isSelected = selected;
}}

0 个答案:

没有答案