Unable to send email with attachments from my app using intents (Gmail)

自闭症网瘾萝莉.ら 提交于 2019-12-11 02:19:26

问题


I'm trying to send files (.log files) contained in a sdcard's folder using Intent. This is the code:

public void sendMail() {
            Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"name.surname@gmail.com"});
            intent.putExtra(Intent.EXTRA_SUBJECT, "Log files");
            intent.putExtra(Intent.EXTRA_TEXT, "body");
            //has to be an ArrayList
            ArrayList<Uri> uris = new ArrayList<Uri>();
            //convert from paths to Android friendly Parcelable Uri's
            if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
                File root = Environment.getExternalStorageDirectory();

                File logfolder = new File(root, "log");


                for (String file : logfolder.list()){
                    Uri u = Uri.parse(file);
                    uris.add(u);
                }
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
                startActivity(Intent.createChooser(intent, new String("Send mail...")));
            }

        }

I choose Gmail from menu. Once Gmail is opend it displays a mail with recipient, subject, text, and attachment files correctly. The mail is sended with no error but i get a status bar notification that says "couldn't show attachment"! In fact recipient receives email correctly but it has no attachment. I'm not able to figure out what is the problem. Why attachments are not sended? Please help me!!


回答1:


Ok. i find the solution. Need to replace this:

for (String file : logfolder.list()){
   Uri u = Uri.parse(file);
   uris.add(u);
}

with this:

for (File file : logfolder.listFiles()){
    Uri u = Uri.fromFile(file);
    uris.add(u);
}


来源:https://stackoverflow.com/questions/15389351/unable-to-send-email-with-attachments-from-my-app-using-intents-gmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!