Unable to attach multiple files to an email in And

2019-07-20 01:46发布

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.

3条回答
孤傲高冷的网名
2楼-- · 2019-07-20 02:09

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.

查看更多
smile是对你的礼貌
3楼-- · 2019-07-20 02:26

You can use an array of intents and startActivity using the intent array.

查看更多
smile是对你的礼貌
4楼-- · 2019-07-20 02:27

if (AndroidUtil.isValidaEmail(email)) {

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

    }   
查看更多
登录 后发表回答