【问题标题】:Android - how to pass a List of Bitmap to another Activity as parameterAndroid - 如何将位图列表作为参数传递给另一个活动
【发布时间】:2016-09-29 19:12:35
【问题描述】:

我有一个 Activity 中的位图列表,我通过异步方法获得。现在我需要将我的位图列表传递给另一个活动。

这是我尝试过的代码,但给了我一个错误...

 List<Bitmap> result = bitmapResult
 Intent intent = new Intent(PhotoList.this,ImageSlider.class);
 intent.putExtra("bitmapList", result);
 startActivity(intent);

它给了我一个错误。

Cannot resolve method 'putExtra(java.lang.String, java.util.List<android.graphics.Bitmap>)'

最好的做法是什么? 以及如何从新 Activity 中检索列表?

提前致谢。

【问题讨论】:

  • “它给我一个错误”是一个非常宽泛的术语。最好发布您遇到的错误。
  • 分享你的日志。
  • 你永远不会传递位图列表。它们太大了,你会遇到内存问题。您所做的是传递对图像的引用,例如指向 SdCard 或 URL 上的位置的字符串

标签: java android android-studio bitmap


【解决方案1】:

由于我们不知道您的位图数组有多大,并且可能会达到分配的传递意图限制。理想的方法是将您的位图作为图像存储在您的 SDCard/Phone 存储中并获取其 Uri。因此您只需要发送 Uri 字符串数组e.g. List&lt;String&gt; 而不是List&lt;Bitmaps&gt;

【讨论】:

  • 好吧。然后我会将字符串列表传递给新的活动,并进行新的异步调用。
【解决方案2】:

在您需要的活动中,您必须收到您之前发送的列表。

List<Bitmap> result = bitmapResult
Intent intent = new Intent(PhotoList.this,ImageSlider.class);
intent.putExtra("bitmapList", result);
startActivity(intent);

接收活动;

List<Bitmap> receive = (List<Bitmap> getIntent.getExtra.get("bitmapList");

之后,您使用“for 循环”来设置您需要的任何项目。 可以用ArrayList,更简单。

【讨论】:

  • 这是不可行的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 2015-10-12
相关资源
最近更新 更多