Android Intent传递大数据报错(java.lang.RuntimeExcption:Failure from system... )且Intent接收值为null

今天想用Intent传一个图片的数据,结果报了错。抓重点,看这两行。

java.lang.RuntimeException: Failure from system
.....
Caused by: android.os.TransactionTooLargeException: data parcel size 6574724 bytes
....

原因是传的数据太大了,然后去网上找答案,说是Intent的缓存机制巴拉巴拉,反正就是传的数据不能超过1mb。
然后就借鉴了一位大佬的…刚想去找…发现被历史淹没了,算了我直接写出来。


首先是创建一个类继承Binder,然后将要传的数据变成里面的变量

public class BigBinder extends Binder {
    private byte[] bytes;

    public BigBinder(byte[] bytes) {
        this.bytes = bytes;
    }

    public byte[] getBytes() {
        return bytes;
    }
}

然后把自己的Binder类放进bundle,再用Intent传过去

Intent intent = new Intent(getContext(), FaceActivity.class);
                        BigBinder bigBinder = new BigBinder(bytes);
                        Bundle bundle = new Bundle();
                        bundle.putBinder("bytes",bigBinder);
                        intent.putExtras(bundle);
                        getContext().startActivity(intent);

ok 这样确实不报错了,正常传过去了。但是在那边儿又报错了,空指针异常!错误信息我就不放了,就是取的Binder他说是空。
错误代码如下

Intent intent = new Intent();
Bundle bundle = intent.getExtras();
BigBinder bigBinder = (BigBinder) bundle.getBinder("bytes");

看到这儿,想kuakua给自己大耳巴子,这是啥呀这是,我不空谁空!我居然新建了一个Intent对象,然后去接收数据,真是栓Q,被气笑了
正确代码

Bundle bundle = getIntent().getExtras();
BigBinder bigBinder = (BigBinder) bundle.getBinder("bytes");

ok,万事大吉了。不知道多年之后的自己看到这个会不会笑晕过去

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值