2

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

首先,我正在准备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();
        }
    }
}

并用于附加和发送电子邮件

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();
        }
    }

我已经Uri通过单独发送每个文件来检查 's 的列表是否准备得很好。但是在使用上面的代码发送多个文件时,我得到了以下信息Exception

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

3 回答 3

7

而不是Intent.ACTION_SEND我使用Intent.ACTION_SEND_MULTIPLE它并且它有效。

来自开发者文档

活动行动:将多个数据传递给其他人。像ACTION_SEND,除了数据是多个。

于 2012-10-04T05:02:01.860 回答
0

您可以使用意图数组和使用意图数组的 startActivity。

于 2012-10-01T17:23:02.043 回答
0

如果(AndroidUtil.isValidaEmail(电子邮件)){

        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);

        BitmapFactory.Options options = new BitmapFactory.Options();  
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;      
        Bitmap bmp = BitmapFactory.decodeByteArray(Produto.getTodosProdutos().get(0).getAnexo(), 0, Produto.getTodosProdutos().get(0).getAnexo().length, options); 

        String path = Images.Media.insertImage(getActivity().getContentResolver(), bmp,"title", null);
        Uri screenshotUri = Uri.parse(path);

        ArrayList<Uri> uris = new ArrayList<Uri>();

        uris.add(screenshotUri);            

        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { emailRemetente });
        intent.putExtra(Intent.EXTRA_SUBJECT, assuntoMontado);
        intent.putExtra(Intent.EXTRA_TEXT, corpoGeralEmail);
        intent.putExtra(Intent.EXTRA_STREAM, uris);
        intent.setType("text/rfc822");
        startActivityForResult(Intent.createChooser(intent, "Choose an Email client :"), 0);

        isEnviado = true;

    }else{

        AndroidUtil.createDialog(getActivity(), R.style.DialogStyle, "Alerta", "Email invalido").show();

    }   
于 2015-08-12T18:01:00.253 回答