色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Android跨進程傳遞大數(shù)據(jù)的方法實現(xiàn)

瀏覽:2日期:2022-09-20 15:25:38

最近要從Service端給Client端傳遞圖片數(shù)據(jù),之前的數(shù)據(jù)都是通過aidl傳遞:

創(chuàng)建 Parcelable文件

ImageData.java

public class ImageData implements Parcelable { private byte[] data; public byte[] getData() { return data; } public ImageData(byte[] dataIn) { this.data = dataIn; } public ImageData(Parcel in) { int arrayLength = in.readInt(); if (arrayLength > 0) { data = new byte[arrayLength]; in.readByteArray(data); } } @Override public void writeToParcel(Parcel dest, int flags) { if (data != null && data.length > 0) { dest.writeInt(data.length); dest.writeByteArray(data); } else { dest.writeInt(0); } } ...}test.aidlinterface test { void sendMessage(ImageData data);}

運行報錯:

android.os.DeadObjectException: Transaction failed on small parcel; remote process probably diedat android.os.BinderProxy.transactNative(Native Method)at android.os.BinderProxy.transact(BinderProxy.java:514)...

原因

這里導(dǎo)致DeadObjectException的原因主要是binder創(chuàng)建的buffer被占滿了:

kernel/msm-4.4/drivers/android/binder_alloc.c 315 if (best_fit == NULL) {...341 pr_err('%d: binder_alloc_buf size %zd failed, no address spacen',342 alloc->pid, size);343 pr_err('allocated: %zd (num: %zd largest: %zd), free: %zd (num: %zd largest: %zd)n',344 total_alloc_size, allocated_buffers, largest_alloc_size,345 total_free_size, free_buffers, largest_free_size);346 eret = ERR_PTR(-ENOSPC);347 goto error_unlock;348 }

傳輸中如果數(shù)據(jù)大于free_buffers,則會拋出DeadObjectException

解決

1.socketsocke傳輸不受大小限制,但實現(xiàn)比較復(fù)雜

2.文件通過文件傳輸比較簡單,但效率差,而且高版本會受到Android系統(tǒng)權(quán)限限制

3.數(shù)據(jù)切割將較大數(shù)據(jù)切割成較小的數(shù)據(jù)傳輸,此方法是兼顧效率,復(fù)雜度較好的方案

定義數(shù)據(jù)體:

public class SliceData implements Parcelable { private byte[] data; private int length; ...}

切割數(shù)據(jù)方法:

public static byte[][] divideArray(byte[] source, int chunkSize) { int totalLength = source.length; int arraySize = (int) Math.ceil(totalLength / (double) chunkSize); byte[][] ret = new byte[arraySize][chunkSize]; int start = 0; int parts = 0; for (int i = 0; i < arraySize; i++) { if (start + chunkSize > totalLength) {System.arraycopy(source, start, ret[i], 0, source.length - start); } else {System.arraycopy(source, start, ret[i], 0, chunkSize); } start += chunkSize; parts++; } return ret; }

將SliceData按順序構(gòu)建發(fā)送:

byte[][] divideData = divideArray(testBytes, 64 * 1024);//64kfor (byte[] item : divideData) { mEmitter.onNext(new SliceData(length, item));}

client接收:

int chunkSize = bytes.length;if(buffer == null) { buffer = new byte[length]; index = 0;}if (index + chunkSize > bodyLength) {//最后一個數(shù)據(jù)塊 System.arraycopy(bytes, 0, buffer, index, bodyLength - index); visualResultData.bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length); buffer = null; index = 0;} else { System.arraycopy(bytes, 0, buffer, index, chunkSize); index += chunkSize;}

4.第三方binder本身也是利用mmap,可以利用實現(xiàn)mmap的框架,比如 MMKV

5.Bitmap如果傳輸?shù)臄?shù)據(jù)是Bitmap,還可以用Bundle的putBinder方案定義binder:

class ImageBinder extends IRemoteGetBitmap.Stub { @Override public Bitmap getBitMap() throws RemoteException { return mBitmap; }}

發(fā)送

Bundle bundle = new Bundle();bundle.putBinder('bitmap', new ImageBinder());intent.putExtras(bundle);

接收:

ImageBinder imageBinder = (ImageBinder) bundle.getBinder('bitmap');Bitmap bitmap = imageBinder.getBitmap();

到此這篇關(guān)于Android跨進程傳遞大數(shù)據(jù)的方法實現(xiàn)的文章就介紹到這了,更多相關(guān)Android跨進程傳遞大數(shù)據(jù)內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 日韩精品中文字幕一区二区三区 | 找个毛片看看 | 手机看a | 污全彩肉肉无遮挡彩色 | 九九精品99久久久香蕉 | 91精品国产9l久久久久 | 91理论片 | 亚洲男人的天堂久久香蕉网 | 国产亚洲一区二区三区 | 欧美一区二区三区精品影视 | 中国一级特黄剌激爽毛片 | 女人张开腿等男人桶免费视频 | 91国语精品自产拍在线观看一 | 成人小视频免费 | 亚洲久草 | 亚洲精品人成在线观看 | 久久国产三级 | 99精品视频免费观看 | 99久久精彩视频 | 亚洲国产欧美国产综合一区 | 久久久久久久久久久大尺度免费视频 | 免费看a级 | 亚洲精品日韩专区在线观看 | 国产三级a三级三级三级 | 美女和男人免费网站视频 | 国产精品夫妇久久 | 热er99久久6国产精品免费 | 手机在线观看精品国产片 | 九九这里只精品视在线99 | 成年人视频在线免费 | 九九视频在线观看视频 | 久爱www免费人成福利播放 | 欧美日韩亚洲综合在线一区二区 | 成人性视频免费网站 | 欧美一区二区三区在线观看 | 国产码一区二区三区 | 欧美特级 | 国产欧美日韩综合精品一区二区 | 成年网站在线在免费播放 | 成人亚洲国产综合精品91 | 国产欧美日韩在线不卡第一页 |