android.os.TransactionTooLargeException: data parcel size 4195216 bytes問題原因與解決

從AActivity跳轉BActivity通過intent.putExtra傳遞數據,結果報android.os.TransactionTooLargeException: data parcel size 4195216 bytes錯誤,意思就是傳輸的數據過大,4195216byte轉換下大概爲4Mb,確實有點大,因爲是從本地文件讀取的String內容,可能文件內容太大。

源碼

遇到問題我們首先看下TransactionTooLargeException這個類的源碼註釋,裏面肯定有說明

通過以下源碼註釋內容可以瞭解到以下幾點信息:

  • 通過Intent傳遞或者返回的數據是存放在一個叫做Binder transaction buffer的緩存區,這個緩衝區的大小爲1Mb(Android 28 Platform),當緩衝區不夠用時就會拋出異常
  • 如果有多個數據傳遞同時進行,是共用緩衝區的1Mb,而不是每一個傳輸各分配1Mb緩存。這就有可能當多個傳輸同時進行時,數據大小小於1M還是拋出TransactionTooLargeException異常
  • 建議的解決方法就是儘可能減小傳輸的數據,至於具體要多效合上也沒個具體的數值,也不可能知道,因爲併發傳輸的數量不固定,但是至少可以肯定的是超過1M肯定會拋異常
/**
 * The Binder transaction failed because it was too large.
 * <p>
 * During a remote procedure call, the arguments and the return value of the call
 * are transferred as {@link Parcel} objects stored in the Binder transaction buffer.
 * If the arguments or the return value are too large to fit in the transaction buffer,
 * then the call will fail and {@link TransactionTooLargeException} will be thrown.
 * </p><p>
 * The Binder transaction buffer has a limited fixed size, currently 1Mb, which
 * is shared by all transactions in progress for the process.  Consequently this
 * exception can be thrown when there are many transactions in progress even when
 * most of the individual transactions are of moderate size.
 * </p><p>
 * There are two possible outcomes when a remote procedure call throws
 * {@link TransactionTooLargeException}.  Either the client was unable to send
 * its request to the service (most likely if the arguments were too large to fit in
 * the transaction buffer), or the service was unable to send its response back
 * to the client (most likely if the return value was too large to fit
 * in the transaction buffer).  It is not possible to tell which of these outcomes
 * actually occurred.  The client should assume that a partial failure occurred.
 * </p><p>
 * The key to avoiding {@link TransactionTooLargeException} is to keep all
 * transactions relatively small.  Try to minimize the amount of memory needed to create
 * a {@link Parcel} for the arguments and the return value of the remote procedure call.
 * Avoid transferring huge arrays of strings or large bitmaps.
 * If possible, try to break up big requests into smaller pieces.
 * </p><p>
 * If you are implementing a service, it may help to impose size or complexity
 * contraints on the queries that clients can perform.  For example, if the result set
 * could become large, then don't allow the client to request more than a few records
 * at a time.  Alternately, instead of returning all of the available data all at once,
 * return the essential information first and make the client ask for additional information
 * later as needed.
 * </p>
 */
public class TransactionTooLargeException extends RemoteException {
    public TransactionTooLargeException() {
        super();
    }

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

解決方法

根據官方的建議就是減小傳輸的數據大小,或者拆分數據分次傳輸,但是如果數據量真的很大且需一次性傳輸有沒解決方法呢,當然有

  1. 數據保存到static全局變量中
  2. 數據保存到本地存儲中,比如本地文件或數據庫,在目標Activity中再提取出來
  3. 通過EventBus.postSticky傳遞包含傳遞數據的粘性事件,在目標Activity中接收該事件提取數據(關於粘性事件參考Android EventBus Sticky Events粘性事件詳解)

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章