概述
使用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的定义中去看看
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.os;
import android.os.RemoteException;
/**
* 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. {@link TransactionTooLargeException} is thrown as a
* heuristic when a transaction is large, and it fails, since these are the transactions
* which are most likely to overfill the transaction buffer.
* </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);
}
}
英语好的话看下注释就明白了了,就是Binder数据传输的时候数据太大了。
大意是说Binder传输依赖的是Bimder传输缓存,该缓存有1Mb的大小限制。而且该1Mb的缓存限制是被应用进程中的所有传输过程所共用的(言下之意是说你单个一次传输限制小于1Mb未必就不会出现上述的崩溃,别的传输过程也要占用部分空间的)。
因此不崩溃的度在哪里?
其实这里给的结论就是说你传输的时候尽量保证数据很小,当然正常情况下你用Intent传递数据的时候就是一些常用数据类型的key-value,本来就没多大,也不用担心触发该崩溃。
但是,当你在页面间利用Intent传输图片的时候呢?是不是就很容易因为传输了较大格式的图片而导致该崩溃呢。此外延伸一下,假如onSaveInstanceState()方法和onRestoreInstanceState()方法触发的时候里面也要有Bundle的,那些存储在其中的数据也是受到1Mb的限制的。
下面就给出解决办法,使用putBinder()方法传递大数据,此时使用的是共享内存而不是Binder传输缓存,因此不受1Mb的限制,但是使用该方法也有要注意的点。
看下注释。
/**
* Inserts an {@link IBinder} value into the mapping of this Bundle, replacing
* any existing value for the given key. Either key or value may be null.
*
* <p class="note">You should be very careful when using this function. In many
* places where Bundles are used (such as inside of Intent objects), the Bundle
* can live longer inside of another process than the process that had originally
* created it. In that case, the IBinder you supply here will become invalid
* when your process goes away, and no longer usable, even if a new process is
* created for you later on.</p>
*
* @param key a String, or null
* @param value an IBinder object, or null
*/
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:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.testintentbigdata.TestIntentTransBIgDataActivity">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_fail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="@color/red"
android:text="使用常规方式传递大数据"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_success"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:background="@color/red"
android:text="使用Binder方式传递大数据"
android:textAllCaps="false"
android:textColor="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn_fail"
tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>
结果页代码如下:
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:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main.testintentbigdata.TestIntentTransBigDataResultActivity">
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="16dp"
android:autoSizeMaxTextSize="48sp"
android:autoSizeMinTextSize="28sp"
android:autoSizeTextType="uniform"
android:gravity="center"
android:lines="1"
android:text="Intent传递大数据成功了呀"
android:textColor="@color/red"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText"
tools:targetApi="o" />
</androidx.constraintlayout.widget.ConstraintLayout>
最终大数据传递成功的结果截图
接到了1048576 Byte的数据
最后
以上就是饱满小蘑菇为你收集整理的Intent传大数据的深入分析的全部内容,希望文章能够帮你解决Intent传大数据的深入分析所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复