• Intent传大数据的深入分析


            使用Intent传递数据大家都知道,但是如果你使用Intent传递大于1Mb的数据时,就一定会报如下的错误

    1. 2022-06-25 11:37:16.601 5901-5901/com.openld.senior E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 1049112)
    2. 2022-06-25 11:37:16.602 5901-5901/com.openld.senior E/AndroidRuntime: FATAL EXCEPTION: main
    3. Process: com.openld.senior, PID: 5901
    4. java.lang.RuntimeException: Failure from system
    5. at android.app.Instrumentation.execStartActivity(Instrumentation.java:1711)
    6. at android.app.Activity.startActivityForResult(Activity.java:5192)
    7. at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
    8. at android.app.Activity.startActivityForResult(Activity.java:5150)
    9. at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
    10. at android.app.Activity.startActivity(Activity.java:5521)
    11. at android.app.Activity.startActivity(Activity.java:5489)
    12. at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.addListeners$lambda-0(TestIntentTransBIgDataActivity.kt:39)
    13. at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.$r8$lambda$57fJAR8O7Q8Thsg13Hl8D6OIbjo(Unknown Source:0)
    14. at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
    15. at android.view.View.performClick(View.java:7125)
    16. at android.view.View.performClickInternal(View.java:7102)
    17. at android.view.View.access$3500(View.java:801)
    18. at android.view.View$PerformClick.run(View.java:27336)
    19. at android.os.Handler.handleCallback(Handler.java:883)
    20. at android.os.Handler.dispatchMessage(Handler.java:100)
    21. at android.os.Looper.loop(Looper.java:214)
    22. at android.app.ActivityThread.main(ActivityThread.java:7356)
    23. at java.lang.reflect.Method.invoke(Native Method)
    24. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
    25. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
    26. Caused by: android.os.TransactionTooLargeException: data parcel size 1049112 bytes
    27. at android.os.BinderProxy.transactNative(Native Method)
    28. at android.os.BinderProxy.transact(BinderProxy.java:510)
    29. at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3847)
    30. at android.app.Instrumentation.execStartActivity(Instrumentation.java:1705)
    31. at android.app.Activity.startActivityForResult(Activity.java:5192) 
    32. at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574) 
    33. at android.app.Activity.startActivityForResult(Activity.java:5150) 
    34. at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560) 
    35. at android.app.Activity.startActivity(Activity.java:5521) 
    36. at android.app.Activity.startActivity(Activity.java:5489) 
    37. at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.addListeners$lambda-0(TestIntentTransBIgDataActivity.kt:39) 
    38. at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity.$r8$lambda$57fJAR8O7Q8Thsg13Hl8D6OIbjo(Unknown Source:0) 
    39. at com.openld.seniorstructure.main.testintentbigdata.TestIntentTransBIgDataActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2) 
    40. at android.view.View.performClick(View.java:7125) 
    41. at android.view.View.performClickInternal(View.java:7102) 
    42. at android.view.View.access$3500(View.java:801) 
    43. at android.view.View$PerformClick.run(View.java:27336) 
    44. at android.os.Handler.handleCallback(Handler.java:883) 
    45. at android.os.Handler.dispatchMessage(Handler.java:100) 
    46. at android.os.Looper.loop(Looper.java:214) 
    47. at android.app.ActivityThread.main(ActivityThread.java:7356) 
    48. at java.lang.reflect.Method.invoke(Native Method) 
    49. at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
    50. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
    51. 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的定义中去看看

    1. /*
    2. * Copyright (C) 2011 The Android Open Source Project
    3. *
    4. * Licensed under the Apache License, Version 2.0 (the "License");
    5. * you may not use this file except in compliance with the License.
    6. * You may obtain a copy of the License at
    7. *
    8. * http://www.apache.org/licenses/LICENSE-2.0
    9. *
    10. * Unless required by applicable law or agreed to in writing, software
    11. * distributed under the License is distributed on an "AS IS" BASIS,
    12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13. * See the License for the specific language governing permissions and
    14. * limitations under the License.
    15. */
    16. package android.os;
    17. import android.os.RemoteException;
    18. /**
    19. * The Binder transaction failed because it was too large.
    20. * <p>
    21. * During a remote procedure call, the arguments and the return value of the call
    22. * are transferred as {@link Parcel} objects stored in the Binder transaction buffer.
    23. * If the arguments or the return value are too large to fit in the transaction buffer,
    24. * then the call will fail. {@link TransactionTooLargeException} is thrown as a
    25. * heuristic when a transaction is large, and it fails, since these are the transactions
    26. * which are most likely to overfill the transaction buffer.
    27. * </p><p>
    28. * The Binder transaction buffer has a limited fixed size, currently 1MB, which
    29. * is shared by all transactions in progress for the process. Consequently this
    30. * exception can be thrown when there are many transactions in progress even when
    31. * most of the individual transactions are of moderate size.
    32. * </p><p>
    33. * There are two possible outcomes when a remote procedure call throws
    34. * {@link TransactionTooLargeException}. Either the client was unable to send
    35. * its request to the service (most likely if the arguments were too large to fit in
    36. * the transaction buffer), or the service was unable to send its response back
    37. * to the client (most likely if the return value was too large to fit
    38. * in the transaction buffer). It is not possible to tell which of these outcomes
    39. * actually occurred. The client should assume that a partial failure occurred.
    40. * </p><p>
    41. * The key to avoiding {@link TransactionTooLargeException} is to keep all
    42. * transactions relatively small. Try to minimize the amount of memory needed to create
    43. * a {@link Parcel} for the arguments and the return value of the remote procedure call.
    44. * Avoid transferring huge arrays of strings or large bitmaps.
    45. * If possible, try to break up big requests into smaller pieces.
    46. * </p><p>
    47. * If you are implementing a service, it may help to impose size or complexity
    48. * contraints on the queries that clients can perform. For example, if the result set
    49. * could become large, then don't allow the client to request more than a few records
    50. * at a time. Alternately, instead of returning all of the available data all at once,
    51. * return the essential information first and make the client ask for additional information
    52. * later as needed.
    53. * </p>
    54. */
    55. public class TransactionTooLargeException extends RemoteException {
    56. public TransactionTooLargeException() {
    57. super();
    58. }
    59. public TransactionTooLargeException(String msg) {
    60. super(msg);
    61. }
    62. }

            英语好的话看下注释就明白了了,就是Binder数据传输的时候数据太大了。

            大意是说Binder传输依赖的是Bimder传输缓存,该缓存有1Mb的大小限制。而且该1Mb的缓存限制是被应用进程中的所有传输过程所共用的(言下之意是说你单个一次传输限制小于1Mb未必就不会出现上述的崩溃,别的传输过程也要占用部分空间的)。

            因此不崩溃的度在哪里?

            其实这里给的结论就是说你传输的时候尽量保证数据很小,当然正常情况下你用Intent传递数据的时候就是一些常用数据类型的key-value,本来就没多大,也不用担心触发该崩溃。

            但是,当你在页面间利用Intent传输图片的时候呢?是不是就很容易因为传输了较大格式的图片而导致该崩溃呢。此外延伸一下,假如onSaveInstanceState()方法和onRestoreInstanceState()方法触发的时候里面也要有Bundle的,那些存储在其中的数据也是受到1Mb的限制的。

            下面就给出解决办法,使用putBinder()方法传递大数据,此时使用的是共享内存而不是Binder传输缓存,因此不受1Mb的限制,但是使用该方法也有要注意的点。

            看下注释。

    1. /**
    2. * Inserts an {@link IBinder} value into the mapping of this Bundle, replacing
    3. * any existing value for the given key. Either key or value may be null.
    4. *
    5. * <p class="note">You should be very careful when using this function. In many
    6. * places where Bundles are used (such as inside of Intent objects), the Bundle
    7. * can live longer inside of another process than the process that had originally
    8. * created it. In that case, the IBinder you supply here will become invalid
    9. * when your process goes away, and no longer usable, even if a new process is
    10. * created for you later on.</p>
    11. *
    12. * @param key a String, or null
    13. * @param value an IBinder object, or null
    14. */
    15. public void putBinder(@Nullable String key, @Nullable IBinder value) {
    16. unparcel();
    17. mMap.put(key, value);
    18. }

            意思是说一般情况在进程中利用Bundle传递数据时,Bundle在使用该数据的进程中存活的时间比创建该Bundle的进程中存活的时间要久。但是如果使用putBinder()方式传递数据时,数据在自定义的Binder中,创建Binder的进程一旦不存在,那Binder在使用它的进程中就会变为不可用。这一点在数据流转与取数据的时候一定要小心。

            下面直接给出我测试的核心代码,通过对比第一个点击事件一定会崩,第二个点击事件跳到了结果页且结果页正确地拿到了大数据。

    触发页面代码如下:

    1. package com.openld.seniorstructure.main.testintentbigdata
    2. import android.content.Intent
    3. import android.os.Binder
    4. import android.os.Bundle
    5. import androidx.appcompat.app.AppCompatActivity
    6. import androidx.appcompat.widget.AppCompatButton
    7. import com.openld.seniorstructure.R
    8. class TestIntentTransBIgDataActivity : AppCompatActivity() {
    9. private lateinit var mBtnFail: AppCompatButton
    10. private lateinit var mBtnSuccess: AppCompatButton
    11. override fun onCreate(savedInstanceState: Bundle?) {
    12. super.onCreate(savedInstanceState)
    13. setContentView(R.layout.activity_test_intent_trans_big_data)
    14. initWidgets()
    15. addListeners()
    16. }
    17. private fun initWidgets() {
    18. mBtnFail = findViewById(R.id.btn_fail)
    19. mBtnSuccess = findViewById(R.id.btn_success)
    20. }
    21. private fun addListeners() {
    22. // 必崩溃
    23. mBtnFail.setOnClickListener {
    24. val intent = Intent(
    25. this,
    26. TestIntentTransBigDataResultActivity::class.java
    27. )
    28. val data = ByteArray(1024 * 1024)
    29. val bundle = Bundle()
    30. bundle.putByteArray("bigData", data)
    31. intent.putExtra("bundle", bundle)
    32. startActivity(intent)
    33. }
    34. // 可以传递大数据
    35. mBtnSuccess.setOnClickListener {
    36. val intent = Intent(
    37. this,
    38. TestIntentTransBigDataResultActivity::class.java
    39. )
    40. val data = ByteArray(1024 * 1024)
    41. val bundle = Bundle()
    42. val bigData = BigData(ByteArray(1024 * 1024))
    43. bundle.putBinder("bigData", bigData)
    44. intent.putExtra("bundle", bundle)
    45. startActivity(intent)
    46. }
    47. }
    48. }
    49. data class BigData(val byteArray: ByteArray) : Binder() {
    50. }

    对应布局xml:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".main.testintentbigdata.TestIntentTransBIgDataActivity">
    8. <androidx.appcompat.widget.AppCompatButton
    9. android:id="@+id/btn_fail"
    10. android:layout_width="0dp"
    11. android:layout_height="wrap_content"
    12. android:layout_marginStart="8dp"
    13. android:layout_marginTop="8dp"
    14. android:layout_marginEnd="8dp"
    15. android:background="@color/red"
    16. android:text="使用常规方式传递大数据"
    17. android:textAllCaps="false"
    18. android:textColor="@color/white"
    19. app:layout_constraintEnd_toEndOf="parent"
    20. app:layout_constraintStart_toStartOf="parent"
    21. app:layout_constraintTop_toTopOf="parent"
    22. tools:ignore="HardcodedText" />
    23. <androidx.appcompat.widget.AppCompatButton
    24. android:id="@+id/btn_success"
    25. android:layout_width="0dp"
    26. android:layout_height="wrap_content"
    27. android:layout_marginStart="8dp"
    28. android:layout_marginTop="8dp"
    29. android:layout_marginEnd="8dp"
    30. android:background="@color/red"
    31. android:text="使用Binder方式传递大数据"
    32. android:textAllCaps="false"
    33. android:textColor="@color/white"
    34. app:layout_constraintEnd_toEndOf="parent"
    35. app:layout_constraintStart_toStartOf="parent"
    36. app:layout_constraintTop_toBottomOf="@+id/btn_fail"
    37. tools:ignore="HardcodedText" />
    38. </androidx.constraintlayout.widget.ConstraintLayout>

    结果页代码如下:

    1. package com.openld.seniorstructure.main.testintentbigdata
    2. import android.os.Bundle
    3. import android.widget.Toast
    4. import androidx.appcompat.app.AppCompatActivity
    5. import com.openld.seniorstructure.R
    6. class TestIntentTransBigDataResultActivity : AppCompatActivity() {
    7. override fun onCreate(savedInstanceState: Bundle?) {
    8. super.onCreate(savedInstanceState)
    9. setContentView(R.layout.activity_test_intent_trans_big_data_result)
    10. val bundle = intent.getBundleExtra("bundle")
    11. val ba = bundle?.getBinder("bigData") as BigData
    12. Toast.makeText(this, "" + ba.byteArray.size / 1024, Toast.LENGTH_SHORT).show()
    13. }
    14. }

    页面布局xml:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".main.testintentbigdata.TestIntentTransBigDataResultActivity">
    8. <androidx.appcompat.widget.AppCompatTextView
    9. android:layout_width="0dp"
    10. android:layout_height="0dp"
    11. android:layout_margin="16dp"
    12. android:autoSizeMaxTextSize="48sp"
    13. android:autoSizeMinTextSize="28sp"
    14. android:autoSizeTextType="uniform"
    15. android:gravity="center"
    16. android:lines="1"
    17. android:text="Intent传递大数据成功了呀"
    18. android:textColor="@color/red"
    19. android:textSize="36sp"
    20. android:textStyle="bold"
    21. app:layout_constraintBottom_toBottomOf="parent"
    22. app:layout_constraintEnd_toEndOf="parent"
    23. app:layout_constraintStart_toStartOf="parent"
    24. app:layout_constraintTop_toTopOf="parent"
    25. tools:ignore="HardcodedText"
    26. tools:targetApi="o" />
    27. </androidx.constraintlayout.widget.ConstraintLayout>

    最终大数据传递成功的结果截图

    接到了1048576 Byte的数据

     

  • 相关阅读:
    FPGA coaxpress 2.0 ip
    视频导出文件太大如何变小?缩小视频这样做
    Django框架之模型层(一)
    在3dmax建好的模型导入到模方映射纹理,这个模型偏移设置该如何设置?默认的话,模型会偏移
    Spring Cloud Gateway微服务网关快速入门
    C语言程序设计(入门)
    Python使用selenium中的CSS_SELECTOR进行搞定复杂多标签定位
    基于springboot的国际化解决方案
    无胁科技-TVD每日漏洞情报-2022-9-15
    景联文科技:高质量的训练数据为高性能自动驾驶汽车提供动力
  • 原文地址:https://blog.csdn.net/ldld1717/article/details/125457470