使用Intent传递数据大家都知道,但是如果你使用Intent传递大于1Mb的数据时,就一定会报如下的错误
2022-06-25 11:37:16.601 5901-5901/com.openld.senior E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1049112)
2022-06-25 11:37:16.602 5901-5901/com.openld.senior E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.openld.senior, PID: 5901
java.lang.RuntimeException: Failure from system
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1711)
at android.app.Activity.startActivityForResult(Activity.java:5192)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at android.app.Activity.startActivityForResult(Activity.java:5150)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
at android.app.Activity.startActivity(Activity.java:5521)
at android.app.Activity.startActivity(Activity.java:5489)
at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.addListeners$lambda-0(TestIntentTransBIgDataActivity.kt:39)
at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.$r8$lambda$57fJAR8O7Q8Thsg13Hl8D6OIbjo(Unknown Source:0)
at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: android.os.TransactionTooLargeException: data parcel size 1049112 bytes
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(BinderProxy.java:510)
at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3847)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1705)
at android.app.Activity.startActivityForResult(Activity.java:5192)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at android.app.Activity.startActivityForResult(Activity.java:5150)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
at android.app.Activity.startActivity(Activity.java:5521)
at android.app.Activity.startActivity(Activity.java:5489)
at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.addListeners$lambda-0(TestIntentTransBIgDataActivity.kt:39)
at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.$r8$lambda$57fJAR8O7Q8Thsg13Hl8D6OIbjo(Unknown Source:0)
at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7125)
at android.view.View.performClickInternal(View.java:7102)
at android.view.View.access$3500(View.java:801)
at android.view.View$PerformClick.run(View.java:27336)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
2022-06-25 11:38:39.289 6145-6145/? E/m.openld.senio: Unknown bits set in runtime_flags: 0x8000
看最关键的点其实很清楚
Caused by: android.os.TransactionTooLargeException: data parcel size 1049112 bytes
就是说你的传输数据太大了,当前的大小达到了1049112 bytes。
那我们进到TransactionTooLargeException的定义中去看看
package android.os;
import android.os.RemoteException;
public class TransactionTooLargeException extends RemoteException {
public TransactionTooLargeException() {
super();
}
public TransactionTooLargeException(String msg) {
super(msg);
}
}
英语好的话看下注释就明白了了,就是Binder数据传输的时候数据太大了。
大意是说Binder传输依赖的是Bimder传输缓存,该缓存有1Mb的大小限制。而且该1Mb的缓存限制是被应用进程中的所有传输过程所共用的(言下之意是说你单个一次传输限制小于1Mb未必就不会出现上述的崩溃,别的传输过程也要占用部分空间的)。
因此不崩溃的度在哪里?
其实这里给的结论就是说你传输的时候尽量保证数据很小,当然正常情况下你用Intent传递数据的时候就是一些常用数据类型的key-value,本来就没多大,也不用担心触发该崩溃。
但是,当你在页面间利用Intent传输图片的时候呢?是不是就很容易因为传输了较大格式的图片而导致该崩溃呢。此外延伸一下,假如onSaveInstanceState()方法和onRestoreInstanceState()方法触发的时候里面也要有Bundle的,那些存储在其中的数据也是受到1Mb的限制的。
下面就给出解决办法,使用putBinder()方法传递大数据,此时使用的是共享内存而不是Binder传输缓存,因此不受1Mb的限制,但是使用该方法也有要注意的点。
看下注释。
public void putBinder(@Nullable String key, @Nullable IBinder value) {
unparcel();
mMap.put(key, value);
}
意思是说一般情况在进程中利用Bundle传递数据时,Bundle在使用该数据的进程中存活的时间比创建该Bundle的进程中存活的时间要久。但是如果使用putBinder()方式传递数据时,数据在自定义的Binder中,创建Binder的进程一旦不存在,那Binder在使用它的进程中就会变为不可用。这一点在数据流转与取数据的时候一定要小心。
下面直接给出我测试的核心代码,通过对比第一个点击事件一定会崩,第二个点击事件跳到了结果页且结果页正确地拿到了大数据。
触发页面代码如下:
package com.openld.seniorstructure.main.testintentbigdata
import android.content.Intent
import android.os.Binder
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import com.openld.seniorstructure.R
class TestIntentTransBIgDataActivity : AppCompatActivity() {
private lateinit var mBtnFail: AppCompatButton
private lateinit var mBtnSuccess: AppCompatButton
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test_intent_trans_big_data)
initWidgets()
addListeners()
}
private fun initWidgets() {
mBtnFail = findViewById(R.id.btn_fail)
mBtnSuccess = findViewById(R.id.btn_success)
}
private fun addListeners() {
// 必崩溃
mBtnFail.setOnClickListener {
val intent = Intent(
this,
TestIntentTransBigDataResultActivity::class.java
)
val data = ByteArray(1024 * 1024)
val bundle = Bundle()
bundle.putByteArray("bigData", data)
intent.putExtra("bundle", bundle)
startActivity(intent)
}
// 可以传递大数据
mBtnSuccess.setOnClickListener {
val intent = Intent(
this,
TestIntentTransBigDataResultActivity::class.java
)
val data = ByteArray(1024 * 1024)
val bundle = Bundle()
val bigData = BigData(ByteArray(1024 * 1024))
bundle.putBinder("bigData", bigData)
intent.putExtra("bundle", bundle)
startActivity(intent)
}
}
}
data class BigData(val byteArray: ByteArray) : Binder() {
}
对应布局xml:
结果页代码如下:
package com.openld.seniorstructure.main.testintentbigdata
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.openld.seniorstructure.R
class TestIntentTransBigDataResultActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test_intent_trans_big_data_result)
val bundle = intent.getBundleExtra("bundle")
val ba = bundle?.getBinder("bigData") as BigData
Toast.makeText(this, "" + ba.byteArray.size / 1024, Toast.LENGTH_SHORT).show()
}
}
页面布局xml:
最终大数据传递成功的结果截图
接到了1048576 Byte的数据



