Android 巧用putBinder方法传递大文件

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

Caused by: android.os.TransactionTooLargeException: data parcel size 1049112 bytes

 就是说你的传输数据太大了,当前的大小达到了1049112 bytes。

 下面就给出解决办法,使用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() {
 
}

接收方:

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()
    }
}

注意:

上面的用法仅限于单进程,如果需要跨进程传递,必须使用AIDL来实现。

具体实现参考:

Android跨进程传图片或者大数据(解决TransactionTooLargeException)_binder传递bitmap-CSDN博客

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值