Android Intent for sending email with attachment [duplicate]

一世执手 提交于 2019-11-26 12:24:50

问题


Possible Duplicate:
Email from internal storage

The email is being received on by the recipient, but without the attachment. Here is the code, any expert knows where did I go wrong?

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...\"));

I am not getting any toast message. Thanks.


回答1:


Try:

Uri.fromFile(file);

instead of:

Uri.parse("file://" + file);

Also, try text/xml for your MIME type, assuming that this is an XML file as your variable name suggests.




回答2:


The file is probably not world readable.

EDIT: indeed. Try doing this:

Uri uri = Uri.parse("file://" + file.getAbsolutePath());


来源:https://stackoverflow.com/questions/6078099/android-intent-for-sending-email-with-attachment

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