1

I want to add multiple images as attachment to an email. Therefore I am trying to add a java.util.ArrayList to an email Intent. The list contains android.net.Uri elements. But it throws a ClassCastException:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to android.os.Parcelable
 at android.os.Bundle.getParcelable(Bundle.java:792)
 at android.content.Intent.getParcelableExtra(Intent.java:5377)
 at android.content.Intent.migrateExtraStreamToClipData(Intent.java:8144)
 at android.content.Intent.migrateExtraStreamToClipData(Intent.java:8124)
 at android.app.Instrumentation.execStartActivity(Instrumentation.java:1505)
 at android.app.Activity.startActivityForResult(Activity.java:3917)
 at android.app.Activity.startActivityForResult(Activity.java:3877)
 at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:843)
 at android.app.Activity.startActivity(Activity.java:4200)
 at android.app.Activity.startActivity(Activity.java:4168)
 at de.zinnet.parkingoffenders.ParkingOffendersActivity.sendEmail(ParkingOffendersActivity.java:135)
 at de.zinnet.parkingoffenders.ParkingOffendersActivity.onOptionsItemSelected(ParkingOffendersActivity.java:103)
 at android.app.Activity.onMenuItemSelected(Activity.java:2908)
 at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:404)

I tried it with different types for the Intent. The method I call to create the Intent and start the Activity:

private void sendEmail() {
    ParkingOffendersListFragment fragment = (ParkingOffendersListFragment) getFragmentManager().findFragmentById(R.id.fragment_list);
    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    String[] to = {settings.getString(getResources().getString(R.string.key_receiver_mail), "")};
    emailIntent .putExtra(Intent.EXTRA_EMAIL, to);

    String subject = settings.getString(getResources().getString(R.string.key_subject_mail), "");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);

    String text = settings.getString(getResources().getString(R.string.key_template_mail), "");
    emailIntent.putExtra(Intent.EXTRA_TEXT, text);

    ArrayList<Parcelable> uris = new ArrayList<>();
    ParkingOffendersListAdapter adapter = fragment.getAdapter();
    for(ParkingOffender parkingOffender : adapter.getSelectedItems()) {
        if(parkingOffender.getImageFilePath() != null) {
            uris.add(Uri.parse(parkingOffender.getImageFilePath()));
        }
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);

    startActivity(Intent.createChooser(emailIntent , "Send email..."));
}

I couldn't find a solution. This did not solve my problem.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Rene
  • 331
  • 7
  • 16

2 Answers2

7

ACTION_SEND supports EXTRA_STREAM, but only for a single Uri. ACTION_SEND_MULTIPLE supports EXTRA_STREAM with an ArrayList<Uri>. So, either change the action or change the extra.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Solved the problem with the attachment. But now I have to add a ArrayList as text. (emailIntent.putExtra(Intent.EXTRA_TEXT, text);). Otherweise I will get a Cast Exception there. – Rene Apr 10 '16 at 11:38
  • 1
    @R.Schmitt: First, bear in mind that `ACTION_SEND*` only has to honor *either* `EXTRA_TEXT` *or* `EXTRA_STREAM`. Beyond that, if you use `ACTION_SEND_MULTIPLE`, you need to send multiple of both `EXTRA_TEXT` and `EXTRA_STREAM`, as `ACTION_SEND_MULTIPLE` implementations will be looking for that, per [the docs](http://developer.android.com/reference/android/content/Intent.html#ACTION_SEND_MULTIPLE). You are also welcome to experiment with the `setClipData()` option and have multiple items in the `ClipData`, with `ACTION_SEND`, but only a subset of `ACTION_SEND` implementations will honor this. – CommonsWare Apr 10 '16 at 11:49
  • Thanks for the wonderfu answer. Send_Multiple did the trick.. :) :) – Mukesh Garg May 24 '16 at 17:34
0

Try putting a ArrayList of Strings as extra to the Intent and then when getting the extras back from the next Activity, just parse it back into Uris.

See this answer

Community
  • 1
  • 1
Jeffalee
  • 1,080
  • 1
  • 7
  • 15