Android Intent用于发送带附件的电子邮件

Mr *_*son 64 java android attachment android-intent

可能重复:
来自内部存储的电子邮件

收件人正在接收电子邮件,但没有附件.这是代码,任何专家都知道我哪里出错了?

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
File file = new File(root, xmlFilename);
if (!file.exists() || !file.canRead()) {
    Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
    finish();
    return;
}
Uri uri = Uri.parse("file://" + file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send email..."));
Run Code Online (Sandbox Code Playgroud)

我没有得到任何祝酒词.谢谢.

Com*_*are 58

尝试:

Uri.fromFile(file);
Run Code Online (Sandbox Code Playgroud)

代替:

Uri.parse("file://" + file);
Run Code Online (Sandbox Code Playgroud)

另外,尝试text/xml使用您的MIME类型,假设这是一个XML文件,如您的变量名所示.


Fem*_*emi 13

该文件可能不是世界可读的.

编辑:的确如此.试着这样做:

Uri uri = Uri.parse("file://" + file.getAbsolutePath());
Run Code Online (Sandbox Code Playgroud)

  • **我**没有告诉你保存外部,我**修复了你的代码,用于从内部存储发送一个附件,以便它现在解决gmail奇怪的问题.不要重新开始出现新错误的新程序,请从您的其他帐户中获取其他问题中的旧程序,并使用内部或外部存储来使用它. (5认同)
  • 是的,要么确保你在logcat中看到的路径是有效的,并且它以/ mnt/sdcard开头(即使它最终在其他地方,以保持GMail满意)或了解ContentProviders.并且请在将来坚持一个帐户和每个主题一个问题. (2认同)