如何传递一个 ArrayList<Bitmap>活动之间 [英] How to pass a ArrayList&lt;Bitmap&gt; between activities

查看:27
本文介绍了如何传递一个 ArrayList<Bitmap>活动之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ArrayList,我使用方法 getBitmapFromAsset() 填充了它,并希望使用 Bundle 通过 Intent 传递它.但是它允许我传递其他 ArrayLists 像 ArrayList 使用:

I have an ArrayList<Bitmap> that I have populated using the method getBitmapFromAsset() and want to pass it via intent using a Bundle. However it allows me to pass other ArrayLists like ArrayList<String> using:

Intent intent = new Intent(myClass.this, Rclass.class);  
Bundle bundle = new Bundle();    
bundle.putStringArrayList("names", (ArrayList<String>) names);  
intent.putExtras(bundle);
startActivity(intent);

但我不知道如何传递 Bitmap 类型的 ArrayList,因为我在 Bundle 中没有看到该选项.关于如何执行此操作的任何想法?

But I don't know how to pass an ArrayList of the type Bitmap as I don't see that option in the Bundle. Any ideas of how to perform this?

推荐答案

将位图本身从一个活动传递到另一个活动的内存效率非常低.如果你的位图是小尺寸的图标可能没问题,但如果是大的位图,你可能会遇到内存不足的异常.

Passing the bitmaps itself from one activity to another is very memory inefficient. It may be OK if your bitmaps are small size icons, but if they are large bitmaps, you may encounter out of memory exception.

你有没有考虑过重构一下,例如使用具有位图 ID(或资产名称)的 HashMap 到位图本身的 WeakReference 的单例.这个单例,我们称之为 BitmapHelper,如果它尚未加载或已被垃圾收集器释放,它将自动从资产中重新加载位图.

Have you consider refactor this a little bit, e.g. Use a singleton that has a HashMap of Bitmap ID (or asset name) to WeakReference of the bitmap itself. This singleton, let's call it BitmapHelper, will auto reload the bitmap from the asset, if it has not yet been loaded or has been freed by the garbage collector.

在您拥有这个 BitmapHelper 之后,接下来就是将字符串数组中的位图 ID/资产名称传递给另一个活动.在其他活动中,您可以从 BitmapHelper 访问位图.

After your have this BitmapHelper, then it's a matter of passing the bitmap id/asset name in a String Array to another activity. From the other activity, you could just access the bitmap from the BitmapHelper.

这篇关于如何传递一个 ArrayList<Bitmap>活动之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆