R.Schmitt R.Schmitt - 3 months ago 64
Java Question

putParcelableArrayListExtra(ArrayList<Uri>) throws ArrayList cannot be cast to android.os.Parcelable

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.

Answer

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.