在Android上发送带有附件的电子邮件编程 [英] Sending an email with attachments programmatically on Android

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

问题描述

我想实现一个按钮,在pressing它会打开默认的电子邮件客户端有一个附件文件。

我下面<一个href=\"http://stackoverflow.com/questions/20355870/explicit-intent-for-android-default-email-client\">this,但我得到的startActivity的错误消息,称它期待的活动参数,而我给它的意图。
我使用21 API和Android 1.1.0工作室,所以也许它是与在链接中提供的答案有何评论?

这是我第四次一天,Android开发者很抱歉,如果我失去了真正的基本的东西。

下面是我的code:

 公共无效sendFileToEmail(文件f){    字符串主题=圈时代;
    ArrayList的&LT;&乌里GT;附件=新的ArrayList&LT;&乌里GT;();
    attachments.add(Uri.fromFile(F));
    意向意图=新意图(Intent.ACTION_SEND_MULTIPLE);
    intent.putExtra(Intent.EXTRA_SUBJECT,学科);
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,附件);
    intent.setClassName(com.android.email,com.android.mail.compose.ComposeActivity);    尝试{
        startActivity(意向);
    }赶上(ActivityNotFoundException E){
        e.printStackTrace();
    }


解决方案

我觉得你的问题是,你没有使用正确的文件路径。

对我来说,以下工作:

 意图emailIntent =新意图(Intent.ACTION_SEND);
emailIntent.setType(text / plain的);
emailIntent.putExtra(Intent.EXTRA_EMAIL,新的String [] {email@example.com});
emailIntent.putExtra(Intent.EXTRA_SUBJECT科目这里);
emailIntent.putExtra(Intent.EXTRA_TEXT正文);
文件根= Environment.getExternalStorageDirectory();
字符串pathToMyAttachedFile =温度/ attachement.xml
档案文件=新的文件(根,pathToMyAttachedFile);
如果(!file.exists()||!file.canRead()){
返回;
}
URI URI = Uri.fromFile(文件);
emailIntent.putExtra(Intent.EXTRA_STREAM,URI);
startActivity(Intent.createChooser(emailIntent,选择电子邮件提供商));

您还需要通过一个manifest文件给用户权限像下面

 &LT;使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/&GT;

I wish to implement a button that upon pressing it will open the default email client with an attachment file.

I am following this, but am getting an error message on the startActivity, saying it is expecting an activity param while I am giving it an intent. I am using API 21 and Android Studio 1.1.0, so perhaps it has something to do with the comment in the answer provided in the link?

This is my fourth day as Android developer so sorry if I am missing something really basic.

Here is my code:

    public void sendFileToEmail(File f){

    String subject = "Lap times";
    ArrayList<Uri> attachments = new ArrayList<Uri>();
    attachments.add(Uri.fromFile(f));
    Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
    intent.setClassName("com.android.email", "com.android.mail.compose.ComposeActivity");

    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }

解决方案

I think your problem is that you are not using the correct file path.

The following works for me:

Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email@example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
String pathToMyAttachedFile = "temp/attachement.xml";
File file = new File(root, pathToMyAttachedFile);
if (!file.exists() || !file.canRead()) {
return;
}
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));

You also need to give the user permission via a manifest file like below

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

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

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