最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年03月17日 09:26:22   作者:R7_Perfect  
這篇文章主要介紹了Android跨進(jìn)程傳遞大數(shù)據(jù)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近要從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.aidl
interface test {
  void sendMessage(ImageData data);
}

運(yùn)行報(bào)錯(cuò):

    android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
        at 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 space\n",
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,則會(huì)拋出DeadObjectException

解決

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

2.文件
通過文件傳輸比較簡(jiǎn)單,但效率差,而且高版本會(huì)受到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);//64k
for (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) {//最后一個(gè)數(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,可以利用實(shí)現(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跨進(jìn)程傳遞大數(shù)據(jù)的方法實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Android跨進(jìn)程傳遞大數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 性能分析工具Systrace的使用及說明

    性能分析工具Systrace的使用及說明

    這篇文章主要介紹了性能分析工具Systrace的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Android數(shù)據(jù)類型之間相互轉(zhuǎn)換系統(tǒng)介紹

    Android數(shù)據(jù)類型之間相互轉(zhuǎn)換系統(tǒng)介紹

    一些初學(xué)Android的朋友可能會(huì)遇到JAVA的數(shù)據(jù)類型之間轉(zhuǎn)換的苦惱;本文將為有這類需求的朋友解決此類問題
    2012-11-11
  • Android 自定義輸入手機(jī)號(hào)自動(dòng)添加分隔符

    Android 自定義輸入手機(jī)號(hào)自動(dòng)添加分隔符

    這篇文章主要介紹了Android 自定義輸入手機(jī)號(hào)自動(dòng)添加分隔符的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-05-05
  • Android開發(fā)之TableLayout表格布局

    Android開發(fā)之TableLayout表格布局

    這篇文章主要為大家詳細(xì)介紹了Android開發(fā)之TableLayout表格布局,表格布局模型是以行列的形式管理子控件,對(duì)TableLayout表格布局感興趣的小伙伴們可以參考一下
    2016-03-03
  • android開機(jī)自啟動(dòng)app示例分享

    android開機(jī)自啟動(dòng)app示例分享

    這篇文章主要介紹了android開機(jī)自動(dòng)啟動(dòng)APP的方法,大家參考使用吧
    2014-01-01
  • Android Studio輕松構(gòu)建自定義模板的步驟記錄

    Android Studio輕松構(gòu)建自定義模板的步驟記錄

    這篇文章主要給大家介紹了關(guān)于Android Studio輕松構(gòu)建自定義模板的相關(guān)資料,文中通過示例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10
  • Android編程中EditText限制文字輸入的方法

    Android編程中EditText限制文字輸入的方法

    這篇文章主要介紹了Android編程中EditText限制文字輸入的方法,涉及Android針對(duì)EditText的監(jiān)聽技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Flutter適配深色模式的方法(DarkMode)

    Flutter適配深色模式的方法(DarkMode)

    這篇文章主要介紹了Flutter適配深色模式的方法(DarkMode),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Android 文件存儲(chǔ)及常見問題解決

    Android 文件存儲(chǔ)及常見問題解決

    這篇文章主要介紹了Android 文件存儲(chǔ)及常見問題解決的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Android實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式

    Android實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式

    由于公司需要一個(gè)帶刻度的進(jìn)度條樣式,因?yàn)榭潭刃枰獎(jiǎng)討B(tài)去改變,所以換背景圖片的方案肯定是不行的,唯一的辦法就是自己繪制一個(gè)進(jìn)度條,下面小編給大家?guī)砹薃ndroid實(shí)現(xiàn)底部帶刻度的進(jìn)度條樣式及實(shí)例代碼,需要的朋友參考下吧
    2019-10-10

最新評(píng)論

崇礼县| 德化县| 邮箱| 上思县| 通道| 武川县| 井冈山市| 理塘县| 松原市| 邵东县| 绵阳市| 德令哈市| 岫岩| 行唐县| 措勤县| 渭源县| 海门市| 奇台县| 乐至县| 浦江县| 军事| 贵港市| 呼图壁县| 徐汇区| 安顺市| 乐陵市| 镇巴县| 遂昌县| 惠水县| 屏山县| 辛集市| 横峰县| 加查县| 关岭| 安福县| 白沙| 遂川县| 句容市| 新泰市| 习水县| 二连浩特市|