繁体   English   中英

Android,.txt电子邮件附件不通过意图发送

[英]Android, .txt email attachment not sending via intent

我正在测试创建.txt文件,然后通过意图将其作为电子邮件附件发送。

创建.txt文件

    try {
        String fileName = "testFileName.txt";
        File root = new File(Environment.getExternalStorageDirectory(), "testDir");
        if (!root.exists()) {
            root.mkdirs();
        }
        File gpxfile = new File(root, fileName);
        FileWriter writer = new FileWriter(gpxfile);
        writer.append("Testing email txt attachment.");
        writer.flush();
        writer.close();
        sendEmail(gpxfile.getAbsolutePath());
    } catch (IOException e) {
        e.printStackTrace();
    }

发送电子邮件

protected void sendEmail(String fileName){
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_SUBJECT, "Test subject");
    i.putExtra(Intent.EXTRA_TEXT, "This is the body of the email");
    i.putExtra(Intent.EXTRA_STREAM, Uri.parse(fileName));
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
}

这一切似乎都很好。 它打开了电子邮件客户端,主题,正文和附件都可见

撰写电子邮件

发送得很好,表明有附件

发送邮件

但是当我打开gmail时,没有显示附件

Gmail,没有附件

我查看电子邮件时的情况相同

Gmail,详细,没有附件

在“已发送”文件夹中查看手机上的电子邮件,也显示没有附件

Android,发送,没有附件

代码是SO上的多个不同帖子的复制和粘贴,看起来他们没有任何问题。 文件在哪里? 它被gmail阻止了吗? 或者根本不发送? 文件不存在吗?

注意:我在清单中设置了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

提前致谢。

问题出在文件路径上。 做了以下更改:

sendEmail(gpxfile); // This is the file itself, not the file path

然后实际发送电子邮件:

protected void sendEmail(File file){
    Uri path = Uri.fromFile(file); // This guy gets the job done!

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_SUBJECT, "Test subject");
    i.putExtra(Intent.EXTRA_TEXT, "This is the body of the email");
    i.putExtra(Intent.EXTRA_STREAM, path); // Include the path
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM