Android 巧用putBinder方法传递大文件

ops/2024/9/25 18:36:06/

  使用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.testintentbigdataimport 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.Rclass TestIntentTransBIgDataActivity : AppCompatActivity() {private lateinit var mBtnFail: AppCompatButtonprivate lateinit var mBtnSuccess: AppCompatButtonoverride 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.testintentbigdataimport android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.openld.seniorstructure.Rclass 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 BigDataToast.makeText(this, "" + ba.byteArray.size / 1024, Toast.LENGTH_SHORT).show()}
}

注意:

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

具体实现参考:

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


http://www.ppmy.cn/ops/35870.html

相关文章

统一大型语言模型和知识图谱:路线图

【摘要】 大型语言模型&#xff08;LLM&#xff09;&#xff0c;如ChatGPT和GPT4&#xff0c;由于其涌现能力和泛化性&#xff0c;正在自然语言处理和人工智能领域掀起新的浪潮。然而&#xff0c;LLM是黑箱模型&#xff0c;通常无法捕捉和获取事实知识。相反&#xff0c;知识图…

CVE-2019-19945漏洞复现 Openwrt针对uhttpd漏洞利用

根据官方漏洞的文档&#xff0c;该漏洞的复现工作我会基于openwrt的18.06.4这个版本进行测试。我选取的环境是渗透测试常用的kali-Linux系统&#xff0c;然后在其中搭建docker环境来完成相应的实验环境的部署。我通过这个核心命令获取docker环境&#xff1a; sudo docker impo…

1W 3KVDC 隔离双输出 DC/DC 电源模块 ——TPD 系列

TPD系列提供双独立输出电压&#xff0c;并且两组电压可以不同&#xff0c;这样就节省一个电源模块&#xff0c;特别适合一块板上有多个不同电压要求的设计&#xff0c;而外形尺寸和TPA一样&#xff0c;工作温度范围广-40℃到 105℃。

Android 蓝牙实战——蓝牙音乐播放进度(二十)

对于蓝牙音乐的开发来说&#xff0c;播放进度是一个比较重要的数据参数&#xff0c;这里我们就来分析一下蓝牙音乐播放进度的相关回调。 一、回调流程 1、AvrcpControllerService 源码位置&#xff1a;/packages/apps/Bluetooth/src/com/android/bluetooth/avrcpcontroller/…

5个好用AI绘画工具,让你秒变艺术家!

AI绘画现在可谓是相当火爆&#xff0c;各种AI绘画工具如雨后春笋般涌出。很多人想自己尝试用AI来创作&#xff0c;却不知道使用什么工具&#xff0c;今天就给大家分享5个好用AI绘画工具&#xff0c;有的只需一段文字便可生成一幅美轮美奂的大作&#xff0c;让你秒变艺术家&…

四化智造MES(WEB)对接打通金蝶云星空余料入库查询(入库记录查询)接口与生产退料单新增接口

四化智造MES&#xff08;WEB&#xff09;对接打通金蝶云星空余料入库查询&#xff08;入库记录查询&#xff09;接口与生产退料单新增接口 接通系统&#xff1a;四化智造MES&#xff08;WEB&#xff09; “MES助力智能制造过程控制:MES管理生产订单的整个生产流程,通过对生产过…

Leetcode 第396场周赛 问题和解法

问题 有效单词 有效单词需要满足以下几个条件&#xff1a; 至少包含3个字符。 由数字0-9和英文大小写字母组成。&#xff08;不必包含所有这类字符。&#xff09; 至少包含一个元音字母。 至少包含一个辅音字母。 给你一个字符串word。如果word是一个有效单词&#xff0c;则…

如何查看MySQL是32位还是64位

如何查看mysql是多少位 - MySQL数据库 - 亿速云 (yisu.com) 进入MySQL的安装路径的bin目录下 C:\Program Files\MySQL\MySQL Server 8.0\bin>mysql -V mysql Ver 8.0.35 for Win64 on x86_64 (MySQL Community Server - GPL) 可以看出&#xff0c;我的是64位的。