putParcelableArrayListExtra(ArrayList <uri>)抛出ArrayList无法强制转换为android.os.Parcelable

时间:2016-04-10 11:18:10

标签: java android android-intent classcastexception

我想添加多个图片作为电子邮件的附件。因此,我尝试将java.util.ArrayList添加到电子邮件Intent。该列表包含android.net.Uri个元素。 但它会引发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)

我尝试使用Intent的不同类型。 我调用的方法是创建Intent并启动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..."));
}

我找不到解决方案。 This没有解决我的问题。

2 个答案:

答案 0 :(得分:3)

ACTION_SEND支持EXTRA_STREAM,但仅适用于单个UriACTION_SEND_MULTIPLE EXTRA_STREAM支持ArrayList<Uri>。因此,要么改变行动,要么改变额外的行动。

答案 1 :(得分:0)

尝试String Intent作为Activity的额外内容,然后在从下一个Uri获取额外内容时,只需将其解析回{{} 1}} S上。

请参阅this answer