Intent transfer data size limitation issue: android.os.TransactionTooLargeException: data parcel size xxxxxxx bytes

Two Activity jumps, we generally use Intent to pass data, but if the passed data is too large, the following exceptions will occur:

2020-10-23 09:52:22.142 5506-5506 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: jdlf_scgl_zp_android.ui.m990_system, PID: 5506
    java.lang.RuntimeException: Failure from system
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
        at android.app.Activity.startActivityForResult(Activity.java:4226)
        at android.app.Activity.startActivityForResult(Activity.java:4185)
        at jdlf_scgl_zp_android.ui.m039_ui_mat_distribution.MatDistributionManager.PickListManager.PickListToLightOperateActivity.jumpToMoveWare(PickListToLightOperateActivity.java:426)
        at jdlf_scgl_zp_android.ui.m039_ui_mat_distribution.MatDistributionManager.PickListManager.PickListToLightOperateActivity.onClick(PickListToLightOperateActivity.java:165)
        at jdlf_scgl_zp_android.ui.m039_ui_mat_distribution.MatDistributionManager.PickListManager.-$$Lambda$qSbxt2tr_nrJz4nnSCo7yUGOiUU.onClick(lambda)
        at android.view.View.performClick(View.java:5675)
        at android.view.View$PerformClick.run(View.java:22641)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6251)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
     Caused by: android.os.TransactionTooLargeException: data parcel size 2435216 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:622)
        at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3191)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
        at android.app.Activity.startActivityForResult(Activity.java:4226) 
        at android.app.Activity.startActivityForResult(Activity.java:4185) 
        at jdlf_scgl_zp_android.ui.m039_ui_mat_distribution.MatDistributionManager.PickListManager.PickListToLightOperateActivity.jumpToMoveWare(PickListToLightOperateActivity.java:426) 
        at jdlf_scgl_zp_android.ui.m039_ui_mat_distribution.MatDistributionManager.PickListManager.PickListToLightOperateActivity.onClick(PickListToLightOperateActivity.java:165) 
        at jdlf_scgl_zp_android.ui.m039_ui_mat_distribution.MatDistributionManager.PickListManager.-$$Lambda$qSbxt2tr_nrJz4nnSCo7yUGOiUU.onClick(lambda) 
        at android.view.View.performClick(View.java:5675) 
        at android.view.View$PerformClick.run(View.java:22641) 
        at android.os.Handler.handleCallback(Handler.java:836) 
        at android.os.Handler.dispatchMessage(Handler.java:103) 
        at android.os.Looper.loop(Looper.java:203) 
        at android.app.ActivityThread.main(ActivityThread.java:6251) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 

The key exception information is these two lines:

java.lang.RuntimeException: Failure from system
Caused by: android.os.TransactionTooLargeException: data parcel size 2435216 bytes

In fact, I knew this conclusion very early, but I never encountered this problem in my impression. I have encountered this problem several times recently, and the reasons are still different:

  • After the user-defined signature page is signed, it will sign a picture and send it back to the previous page. Because the bitmap object is passed through the picture, the amount of data is too large;
  • To pass the collection ArrayList, this is what we often use. At first, we didn't think it was a problem with ArrayList. After looking at the log, there are more than 1200 data in the List, and the serialized List has reached 1.5M;
  • The Activity+ Viewpager+Fragment form uses Bundle when transferring data to Fragment, and the amount of data carried in the Bundle is too large.

1. The first solution is to store the bitmap as a byte array, and then pass it through the Intent:

 ByteArrayOutputStream baos = new ByteArrayOutputStream();
 mBitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
 byte[] bytesArrayBmp = baos.toByteArray();
 intent.putExtra(SIGN_PIC_BITMAP, bytesArrayBmp);

So why can Bitmap be transferred into a byte array? My personal understanding is that Bitmap implements the Parcelable serialization interface, so all the data of Bitmap will be serialized to memory when transferring, so it will exceed the 1M limit . The byte array corresponds to the address value in the memory. When it is passed, it is actually the address value, so it will not exceed the limit. The author's second method of passing the map object (or HashMap as others call it) is also the address value in the passed memory, which is strictly the same as passing the byte array.

2. The second case: the data is saved in local storage, and then extracted from the target Activity.

3. The third need to use the bundle.clear() method to clear the bundle after obtaining the data;

Bundle bundle = getArguments();
//获取解析结果集
//String strAsModel = bundle.getString("fdModelList");
//获取数据之后要清空bundle,否则数据量大时跳转到其他Activity会报异常
bundle.clear();

Of course, in the project, I have already put an end to Activity to pass big data to Fragment, and the required data is directly obtained in Fragment.

Guess you like

Origin blog.csdn.net/beita08/article/details/109237173