我应该如何将位图的数组列表从一个活动发送到另一个活动?



发送活动中的代码:

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

接收活动中的代码:

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

转到接收活动后,应用程序立即崩溃。请帮忙。

您不应该将此类数据从活动 A 发送到 B.在它们之间使用某种中间类,您可以在其中设置此数据然后检索,例如存储库。捆绑包和意图不是为大数据设计的。还可以考虑保留 Id 或 URI-s 并从其他活动访问它们,而不是直接发送普通位图

文档: https://developer.android.com/reference/android/os/TransactionTooLargeException

Binder 事务缓冲区具有有限的固定大小,目前为 1Mb,由流程正在进行的所有事务共享。因此,当正在进行许多事务时,即使大多数单个事务的大小适中,也可能引发此异常。

位图扩展了 Parcelable,这意味着您可以提供如下列表:

ArrayList<Bitmap> bitmapList = new ArrayList<Bitmap>();
// Poupulate list here
Intent intent = new Intent();
intent.putParcelableArrayListExtra("list", bitmapList);

然后,您可以在接收活动中将其转换为位图[]:

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

但请记住,在你的意图中放太多东西通常是不好的做法。最好存储数据(数据库、文件系统、单例,...(并传递 URI 或 ID。

最新更新