无法多个文件附加到Android的电子邮件 [英] Unable to attach multiple files to an email in Android

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

问题描述

我要多个文件附加到电子邮件中。

首先我乌里preparing名单的。

 无效prepareListOfUri()
{
    listOfUri =新的ArrayList<&乌里GT;();
    文件fil​​eTemp =新的文件(android.os.Environment.getExternalStorageDirectory(),BuyNowImages);
    fileTemp.mkdirs();    的for(int i = 0; I< listOfImageView.size();我++)
    {
        尝试
        {
            ByteArrayOutputStream BOS =新ByteArrayOutputStream();
            可绘制可绘制= listOfImageView.get(I).getDrawable();
            位图bitmapPicked =((BitmapDrawable)抽拉).getBitmap();
            bitmapPicked.com preSS(比较pressFormat.JPEG,75,BOS);
            字节[] =图像bos.toByteArray();
            档案文件=新的文件(fileTempbuynow_product+ I +.JPG);
            file.createNewFile();
            //写入文件中的字节
            FileOutputStream中FO =新的FileOutputStream(文件);
            fo.write(图片);
            URI URI = Uri.parse(文件://+ file.getAbsolutePath());
            listOfUri.add(ⅰ,URI);
        }赶上(例外五)
        {
            // TODO:处理异常
            e.printStackTrace();
        }
    }
}

和用于连接功放&;发送电​​子邮件

 无效sendMultipleAttachments()
    {
        尝试
        {
            意图int​​entEmail =新意图(Intent.ACTION_SEND);
            intentEmail.setType(text / plain的);
            的String [] =收件人新的String [] {};            intentEmail.putExtra(Intent.EXTRA_EMAIL,收件人);
            intentEmail.putExtra(Intent.EXTRA_SUBJECT,电子邮件的主题);
            intentEmail.putExtra(Intent.EXTRA_TEXT,电子邮件的身体);
            intentEmail.setType(图像/ JPEG);
            intentEmail.putParcelableArrayListExtra(Intent.EXTRA_STREAM,listOfUri);
            startActivity(intentEmail);
        }赶上(android.content.ActivityNotFoundException前)
        {
            Toast.makeText(这一点,有没有安装的电子邮件客户端。Toast.LENGTH_SHORT).show();
            ex.printStackTrace();
        }赶上(例外五)
        {
            e.printStackTrace();
            Toast.makeText(这一点,异常+ e.getMessage(),Toast.LENGTH_SHORT).show();
        }
    }

我已经检查 URI的列表的是$ P $通过单独发送的每个文件完全ppared。但是,通过使用上述code我获得以下异常发送多个文件

 键android.intent.extra.STREAM预期Parcelable但其值是一个java.util.ArrayList中。默认值为<&空GT;被退回。


解决方案

相反,我使用了 Intent.ACTION_SEND Intent.ACTION_SEND_MULTIPLE 和它的工作。

开发者文档


  

活动事件:多个数据传递给别人。像 ACTION_SEND ,不同的是数据多。


I want to attach multiple files to an email.

First I am preparing list of Uri's.

void prepareListOfUri()
{
    listOfUri = new ArrayList<Uri>();
    File fileTemp = new File(android.os.Environment.getExternalStorageDirectory(), "BuyNowImages");
    fileTemp.mkdirs();

    for (int i = 0; i < listOfImageView.size(); i++)
    {
        try
        {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            Drawable drawable = listOfImageView.get(i).getDrawable();
            Bitmap bitmapPicked = ((BitmapDrawable) drawable).getBitmap();
            bitmapPicked.compress(CompressFormat.JPEG, 75, bos);
            byte[] image = bos.toByteArray();
            File file = new File(fileTemp, "buynow_product" + i + ".jpg");
            file.createNewFile();
            // write the bytes in file
            FileOutputStream fo = new FileOutputStream(file);
            fo.write(image);
            Uri uri = Uri.parse("file://" + file.getAbsolutePath());
            listOfUri.add(i, uri);
        } catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

And for attaching & sending email

void sendMultipleAttachments()
    {
        try
        {
            Intent intentEmail = new Intent(Intent.ACTION_SEND);
            intentEmail.setType("text/plain");
            String[] recipients = new String[] { "" };

            intentEmail.putExtra(Intent.EXTRA_EMAIL, recipients);
            intentEmail.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
            intentEmail.putExtra(Intent.EXTRA_TEXT, "body of email");
            intentEmail.setType("image/jpeg");
            intentEmail.putParcelableArrayListExtra(Intent.EXTRA_STREAM, listOfUri);
            startActivity(intentEmail);
        } catch (android.content.ActivityNotFoundException ex)
        {
            Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
            ex.printStackTrace();
        } catch (Exception e)
        {
            e.printStackTrace();
            Toast.makeText(this, "Exception " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

I have checked the list of Uri's is prepared perfectly by sending the each file individually. But on sending multiple files by using above code I get following Exception.

Key android.intent.extra.STREAM expected Parcelable but value was a java.util.ArrayList.  The default value <null> was returned.

解决方案

Instead of Intent.ACTION_SEND I used Intent.ACTION_SEND_MULTIPLE and it worked.

From the developer documentation:

Activity Action: Deliver multiple data to someone else. Like ACTION_SEND, except the data is multiple.

这篇关于无法多个文件附加到Android的电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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