我应该如何从一个活动中向另一个活动发送Bitmaps的ArrayList?I

问题描述 投票:0回答:2

发送活动中的代码。

    Intent intent = new Intent(MainActivity.this,Main2Activity.class);
    intent.putParcelableArrayListExtra("bitmaps",  bitmapArrayList);
    startActivity(intent);

在接收活动中的代码:

    Intent intent = getIntent();
    bitmapArrayList =  intent.getParcelableArrayListExtra("bitmaps");

一进入接收活动,应用程序就崩溃了。请帮助我们。

android arraylist android-intent bitmap parcelable
2个回答
2
投票

你不应该把这种数据从Activity A发送到B,应该在它们之间使用某种媒介类,在那里你可以设置这些数据,然后检索,比如repository。Bundles和Intents不是为大数据设计的。也可以考虑保留Ids或URI-s,并从另一个活动中访问它们,而不是直接发送纯位图。

文档。https:/developer.android.comreferenceandroidosTransactionTooLargeException。

Binder事务缓冲区有一个有限的固定大小,目前是1Mb,它被进程中的所有事务共享。因此,当有许多事务正在进行时,即使大多数单个事务的大小适中,也会抛出这个异常。


1
投票

Bitmap扩展了Parcelable,这意味着你可以提供一个这样的列表。

 ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
// Poupulate list here

Intent intent = new Intent();
intent.putParcelableArrayListExtra("list", bitmapList);

然后你可以在你的接收活动中把它转换为一个Bitmap[]:

Bitmap[] bitmapArray = bitmapList.toArray(new Bitmap[bitmapList.size()]);

但请记住,一般来说,在你的意图中放太多东西是不好的做法。最好是将你的数据存储起来(DB、文件系统、单体......),然后传递URI或ID。

© www.soinside.com 2019 - 2024. All rights reserved.