从资源文件夹发送带有附件的电子邮件 [英] Sending Email with attachment from Asset folder

查看:177
本文介绍了从资源文件夹发送带有附件的电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    //EMAIL SENDING CODE  FROM ASSET FOLDER
    email = editTextEmail.getText().toString();
    subject = editTextSubject.getText().toString();
    message = editTextMessage.getText().toString();
    final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("file/html");
    emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.example.deepa.xmlparsing/file:///android_assets/Combination-1.html"));
    startActivity(Intent.createChooser(emailIntent, "Send email using"));

最后,我从资产文件夹(Combination-1.html)中获取文件.

Finally, I'm getting the file from asset folder (Combination-1.html).

正在得到

找不到运行时错误文件异常.

还有其他发送文件附件的方式吗?

Is there any other way to send file attachment?

推荐答案

发送电子邮件的最简单方法是创建ACTION_SEND类型的Intent:

The easiest way to send an email is to create an Intent of type ACTION_SEND :

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
intent.putExtra(Intent.EXTRA_TEXT, "Attachment test");

要附加单个文件,请向Intent添加一些扩展数据:

To attach a single file, add some extended data to Intent :

intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/path/to/file")));
intent.setType("text/html");

或使用Html.fromHtml()构建html内容:

Or use Html.fromHtml() to build html content:

intent.putExtra( Intent.EXTRA_TEXT,
                 Html.fromHtml(new StringBuilder()
                 .append("<h1>Heading 1</h1>")
                 .append("<p><a>http://www.google.com</a></p>")
                 .toString()));

对于多个附件:

Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple");
intent.putExtra(Intent.EXTRA_TEXT, "multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(new File("/path/to/first/file")));
uris.add(Uri.fromFile(new File("/path/to/second/file")));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

完成对传递意图的startActivity()的调用.

Finish with a call to startActivity() passing intent.

这篇关于从资源文件夹发送带有附件的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆