亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android跨進程傳遞大數據的方法實現

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

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

創建 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)...

原因

這里導致DeadObjectException的原因主要是binder創建的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 }

傳輸中如果數據大于free_buffers,則會拋出DeadObjectException

解決

1.socketsocke傳輸不受大小限制,但實現比較復雜

2.文件通過文件傳輸比較簡單,但效率差,而且高版本會受到Android系統權限限制

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

定義數據體:

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

切割數據方法:

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按順序構建發送:

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) {//最后一個數據塊 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,可以利用實現mmap的框架,比如 MMKV

5.Bitmap如果傳輸的數據是Bitmap,還可以用Bundle的putBinder方案定義binder:

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

發送

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

接收:

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

到此這篇關于Android跨進程傳遞大數據的方法實現的文章就介紹到這了,更多相關Android跨進程傳遞大數據內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 亚洲精品国产高清不卡在线 | 国产中文视频 | 国产人成午夜免视频网站 | 哪里可以免费看毛片 | 91短视频黄 | 欧美精品一区二区三区免费 | 国产福利兔女郎在线观看 | 一国产一级淫片a免费播放口 | 国产精品一区二区av | 亚洲一区在线观看视频 | 噜噜噜天天躁狠狠躁夜夜精品 | 91精品国产爱久久久久久 | 欧美成人免费草草影院视频 | 伊人狠狠丁香婷婷综合色 | 一个人看的ww免费视频 | 亚洲1024| 国产高清精品毛片基地 | 国产精彩视频在线 | 91久久视频| 伊人久久免费视频 | 亚洲综合专区 | 久久久久久国产a免费观看黄色大片 | 女人被免费看在线看 | 在线国产日韩 | 欧美成人黑人性视频 | 综合亚洲精品一区二区三区 | 国产综合在线观看视频 | 久久99热不卡精品免费观看 | 亚洲欧美日韩另类在线 | 欧美一区二区手机在线观看视频 | 免费看黄色片网站 | 免费特黄 | 国产大片免费看 | www.亚洲黄色| 污污视频在线观看黄 | 国产成人精品免费视频大全可播放的 | jul724青木玲中文字幕 | 日韩精品久久久久影院 | 国产黄的网站免费 | 日韩一级视频免费观看 | 99欧美精品 |