在Android上发送带有附件的电子邮件编程附件、电子邮件、Android

2023-09-06 07:18:38 作者:遥遥江上客

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

我下面this,但我得到的startActivity的错误消息,称它期待的活动参数,而我给它的意图。我使用21 API和Android 1.1.0工作室,所以也许它是与在链接中提供的答案有何评论?

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

下面是我的code:

 公共无效sendFileToEmail(文件f){    字符串主题=圈时代;    ArrayList的<&乌里GT;附件=新的ArrayList<&乌里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();    } 
Android实现带附件的邮件发送功能

解决方案

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

对我来说,以下工作:

 意图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文件给用户权限像下面

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/> 

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"/>

 
精彩推荐
图片推荐