【问题标题】:Android, .txt email attachment not sending via intentAndroid,.txt 电子邮件附件未通过意图发送
【发布时间】:2015-10-23 13:27:45
【问题描述】:

我正在测试创建一个 .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时,没有显示附件

当我查看电子邮件时,同样的故事

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

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

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

提前致谢。

【问题讨论】:

  • 不是在File 上创建FileWriter,而是在File 上从FileOutputStream 创建一个FileWriter。然后,在您拨打flush()close()FileWriter 之间,拨打getFD().sync()FileOutputStream。这是一个阻塞调用,直到所有字节都写入磁盘后才会返回。看看是否有帮助。另请注意,ACTION_SEND 支持 EXTRA_TEXTEXTRA_STREAMIntent,但不能同时支持两者,因此如果某些应用忽略其中之一,请不要感到惊讶。
  • @CommonsWare 感谢您的意见。我发布了一个对我有用的解决方案 - 这只是路径的问题。并感谢有关 ACTION_SEND 的提示。 Android Docs 两者都包含,那么如何确保两者都包含?
  • “Android Docs 包括这两者”——叹息。我会努力解决这个问题。引用the real docs:“getType() 是正在发送的数据的 MIME 类型。get*Extra 可以有一个 EXTRA_TEXT 或 EXTRA_STREAM 字段,包含要发送的数据。如果使用 EXTRA_TEXT,MIME 类型应该是“text/ plain";否则应该是EXTRA_STREAM中数据的MIME类型。"
  • "如何确保两者都包含在内?" ——嗯……在所有开发者都同意你的要求之前扣留人质?否则,你不能,因为这取决于接收者如何处理请求。

标签: android email android-intent gmail filewriter


【解决方案1】:

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

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

【讨论】:

  • 在搜索有效示例 2 天后,我找到了您的工作解决方案。非常感谢比雷尔!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 2013-02-21
  • 2013-01-02
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多