且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将包含位图的对象传递给另一个活动

更新时间:2023-11-09 23:30:16

在两个活动之间传递位图不是一个好主意.尝试这样做会得到TransactionTooLargeException.交易数据的最大限制为1MB左右,位图可能会轻易地使其超调.这可能会导致崩溃.

Its not a great idea to pass Bitmap between two activities. You would get TransactionTooLargeException when you attempt to do so. Maximum limit for transaction data is around 1MB and Bitmap could easily overshoot it. This could lead to crash.

您可以通过以下代码使用在onActivityResult()中获得的URI:

You can just use the URI that you are getting in onActivityResult() via following code:

Uri selectedImage = data.getData();

在活动之间传递此URI.然后,您可以按需使用URI加载图像,而不是传输整个Bitmap对象.

Pass this URI between the activities. You can then load image using the URI on-demand rather than transfering whole Bitmap object.

@SabaJafarzade谢谢指出,重用Uri.

@SabaJafarzade Thank you pointing out, to reuse the Uri.