您如何使用捆绑包在 android 活动之间传递图像(位图)? [英] how do you pass images (bitmaps) between android activities using bundles?

查看:26
本文介绍了您如何使用捆绑包在 android 活动之间传递图像(位图)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个活动从图库中选择一个图像,并将其作为位图检索,就像示例一样:这里

Suppose I have an activity to select an image from the gallery, and retrieve it as a BitMap, just like the example: here

现在,我想传递这个 BitMap 以在 ImageView 中用于另一个活动.我知道捆绑包可以在活动之间传递,但我如何将此 BitMap 存储到捆绑包中?

Now, I want to pass this BitMap to be used in an ImageView for another activity. I am aware bundles can be passed between activities, but how would I store this BitMap into the bundle?

或者我应该采取其他方法吗?

or is there another approach I should take?

推荐答案

我强烈推荐一种不同的方法.

I would highly recommend a different approach.

如果您真的想这样做,这是可能的,但它会消耗大量内存并且速度也很慢.如果您的手机较旧且位图很大,则可能无法使用.您可以将它作为额外的传递,例如 intent.putExtra("data", bitmap).位图实现了 Parcelable,所以你可以把它放在一个额外的地方.同样,一个bundle有putParcelable.

It's possible if you REALLY want to do it, but it costs a lot of memory and is also slow. It might not work if you have an older phone and a big bitmap. You could just pass it as an extra, for example intent.putExtra("data", bitmap). A Bitmap implements Parcelable, so you can put it in an extra. Likewise, a bundle has putParcelable.

如果你想在活动之间传递它,我会将它存储在一个文件中.这样效率更高,您的工作更少.您可以使用 MODE_PRIVATE 在您的数据文件夹中创建任何其他应用都无法访问的私人文件.

If you want to pass it inbetween activities, I would store it in a file. That's more efficient, and less work for you. You can create private files in your data folder using MODE_PRIVATE that are not accessible to any other app.

这篇关于您如何使用捆绑包在 android 活动之间传递图像(位图)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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