如何将位图列表有效地从一个活动传递到另一个活动?

我有一个包含 3 个对象的列表,我需要将此列表从一个活动传递到 另一个活动。MatOpencvAndroid

我不知道如何在活动之间共享此垫子列表,因此我将这些对象转换为位图,现在我必须处理位图。Mat

我知道该类默认实现,所以我像这样使用BitmapParcelableputParcelableArrayList

Intent intent = new Intent(InputProcessingActivity.this, CompareActivity.class);
intent.putParcelableArrayListExtra("", mats);
startActivity(intent);

但是我只是变得黑暗并保持这种状态,我认为这是因为位图对象太大而发生这种情况。Application

您能提出一个解决方案来实现这一目标吗?


浮云间
浏览 97回答 2
2回答

尚方宝剑之说

如果您打算仅在运行时保留该列表,我建议您将该列表存储在单例对象中,然后在下一个活动中读取它。这是一种非常简单,直接且CPU / IO友好的方式来解决您的问题。例:public class BitmapDTO {&nbsp; &nbsp; private static BitmapDTO instance;&nbsp; &nbsp; public static BitmapDTO getInstance() {&nbsp; &nbsp; &nbsp; &nbsp; if (instance == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instance = new BitmapDTO();&nbsp; &nbsp; &nbsp; &nbsp; return instance;&nbsp; &nbsp; }&nbsp; &nbsp; private List<Bitmap> bitmaps;&nbsp; &nbsp; private BitmapDTO() { }&nbsp; &nbsp; public void setBitmaps(List<Bitmap> bitmaps) {&nbsp; &nbsp; &nbsp; &nbsp; this.bitmaps = bitmaps;&nbsp; &nbsp; }&nbsp; &nbsp; public List<Bitmap> getBitmaps() {&nbsp; &nbsp; &nbsp; &nbsp; return bitmaps;&nbsp; &nbsp; }}源活动:BitmapDTO.getInstance().setBitmaps(myBitmaps);Intent intent = new Intent(InputProcessingActivity.this, CompareActivity.class);startActivity(intent);最后,关于您的目标活动:private List<Bitmap> myBitmaps;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.my_layout);&nbsp; &nbsp; myBitmaps = BitmapDTO.getInstance().getBitmaps();&nbsp; &nbsp; // Do your stuff}

慕妹3146593

将 s 保存到某些文件,并通过意图传递文件的路径。Bitmap
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java