Detailed explanation of Android Intent transfer data size limit

  • 2021-11-02 02:48:10
  • OfStack

Preface

In sendBroadcast, startActivity, we will use Intent.

Intent can carry data such as primitive type data int, Boolean, or String, or serialized objects, Parcelable, and Serializable.
When Intent passes data, an exception may occur if the data is too large. For example, App flashes back, Intent sends unsuccessfully, logcat reports errors and so on.

This involves one problem: Intent passes data size limit.

How much data can Intent carry?

Exceptions may occur when using Intent to transfer data

Pass in 1 Parcelable object in Intent; For example, pass in 1 bitmap object.

Code reference: github. com/RustFisher/…


  Bitmap b1 = Bitmap.createScaledBitmap(srcBmp, dstWid, dstHeight, false);
  Intent intent = new Intent(MSG_INTENT);
  intent.putExtra(K_PIC, b1);

The reason for choosing bitmap is that Bitmap implements the Parcelable interface, and the memory size is known through getByteCount ().

When sendBroadcast, the following information is reported

V/ActivityManager: Broadcast: Intent { act=intent_bi flg=0x10 (has extras) } ordered=false userid=0 callerApp=ProcessRecord{27aeaaf5 31217:com.rustfisher.basic4/u0a113}
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!
W/BroadcastQueue: Failure sending broadcast Intent { act=intent_bi flg=0x10 (has extras) }
android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:504)
at android.app.ApplicationThreadProxy.scheduleRegisteredReceiver(ApplicationThreadNative.java:1170)
at com.android.server.am.BroadcastQueue.performReceiveLocked(BroadcastQueue.java:576)
at com.android.server.am.BroadcastQueue.deliverToRegisteredReceiverLocked(BroadcastQueue.java:848)
at com.android.server.am.BroadcastQueue.processNextBroadcast(BroadcastQueue.java:917)
at com.android.server.am.BroadcastQueue$BroadcastHandler.handleMessage(BroadcastQueue.java:254)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.os.HandlerThread.run(HandlerThread.java:61)
at com.android.server.ServiceThread.run(ServiceThread.java:46)

Look at the exception class TransactionTooLargeException, which inherits RemoteException


package android.os;
public class TransactionTooLargeException extends RemoteException {
  public TransactionTooLargeException() {
    super();
  }

  public TransactionTooLargeException(String msg) {
    super(msg);
  }
}

Tracing Binder, its transactNative method will report RemoteException


public native boolean transactNative(int code, Parcel data, Parcel reply,
      int flags) throws RemoteException;

Throwing exceptions is related to Binder.

The size of information carried by Intent is limited by Binder

The size of information carried by Intent is actually limited by Binder. The title of this article can also be changed to "Binder Transfer Data Size Limit".

Data is stored in the Binder delivery cache in the form of Parcel objects.

If the data or return value is larger than the passed buffer, the pass call fails and an TransactionTooLargeException exception is thrown.

The Binder delivery cache has a limited size, typically 1Mb. But it shares cache space with all transports in one process.

TransactionTooLargeException exceptions may be thrown when multiple places are transmitting, even if the data they each transmit does not exceed the size limit.

When using Intent to pass data, 1Mb is not the upper security limit. Because other transfer work may be being handled in Binder.

This upper limit may be different for different models and system versions.

Similar problems with Binder may be encountered elsewhere, such as onSaveInstanceState (@ NonNull Bundle outState).

Why does Binder want to limit the size of transmitted data

Personally, as a way of IPC, Binder is not designed to transmit a large amount of data.

To transfer a large amount of data, you can consider methods such as URL.

Reference

stackoverflow. com/questions/8 … developer. android. com/reference/a …

Summarize


Related articles: