android中資料的分包

2021-07-28 19:56:58 字數 1958 閱讀 1367

之前在開發中,遇到了乙個問題,就是在專案assets下有乙個apk需要在應用啟動時安裝到裝置中,實現的思路是需要首先將apk拷貝到sdcard中,然後執行安裝操作,還有乙個問題就是專案中的資料庫也需要放在sdcard中,這個apk大約有5m左右,我的資料庫大約有30m左右,接下來我就按照普通的i/o流拷貝:

public

static

boolean

copydatatosd(context context, string filename, file stroutfile)

return

true;

} catch (exception e) finally

if (myinput != null)

myinput.close();

} catch (exception e)

}}

但是在執行上述**時,卻報錯了,經過查詢才知道android2.3之後要求從apk拷貝出去的資料必須小於1m,坑爹呀!!!

那麼,我們只能將資料分解了!,直接看完整的**:

public

static listsplitbigfile(context context, string filename, string formattype, string dirname)

while (true)

list.add(file.getpath());

fileoutputstream fos = new fileoutputstream(file);

for (int i = 0; i < split / buf.length; i++)

}num++;

}} catch (exception e)

return list;

}public

static boolean mergefile(object partfilelist, string dst, string formattype, string dirname)

out.flush();

in.close();}}

// 把所有小檔案都進行寫操作後才關閉輸出流,這樣就會合併為乙個檔案了

out.close();

deletefolder(sdcardutils.getsdcardpath() + file.separator + dirname);

return

true;

} catch (exception e)

}public

static

void

deletefolder(string folderpath) else

}localfolder.delete();

}}

通過上面的**可以看出實現的思路是:

a.將原始檔平均1m大小地分解讀取,然後每m流分別寫入sdcard中乙個小檔案中,檔案路徑存放在乙個集合中,這些小檔案的命名分別問類似編號:***1.db,***2.db….等等;

b.在sdcard中建立乙個要生成的目標檔案,然後通過遍歷這些小檔案,將每個小檔案寫入目標檔案中;

c.最後關閉流物件,刪除這些小檔案。

最後,在需要呼叫的地方:

//第乙個引數是上下文,第二個引數是assets下資料報的名字,第三個引數表示資料報的字尾,第四個引數表示要在sdcard中臨時存放分解的小檔案的資料夾

liststrings =copydatatosdutils.splitbigfile(mainactivity.this, "***", ".apk", "***apk");

boolean b =copydatatosdutils.mergefile(strings.toarray(), apkpath, ".apk", "hdpapk");

if (b) else

Android藍芽接收分包資料

最近在開發乙個安卓和藍芽光譜儀裝置通訊的程式,用的是傳統藍芽,下位機藍芽傳送的光譜資料比較大,分成了15個包傳送。其中14個包大小290個byte,最後1個包大於100個byte,而且一整個資料報由資料頭 資料長度 資料 資料尾這幾個部分組成,所以接受的時候做了乙個判斷,定義了乙個大容量陣列,用笨方...

Android分包方案multidex

我們的demo圖如下,我們根據該圖和dex檔案反編譯的結果分析分包情況。或者這樣解決 一些在二級dex載入之前,可能會被呼叫到的類 比如靜態變數的類 需要放在主dex中.否則會classnotfounderror.通過修改gradle,可以顯式的把一些類放在main dex中.afterevalua...

Android 差分包製作

1 差分包製作 每次編譯out target product autolink 6dl obj packaging target files intermediates 路徑下都會生成 zip的公升級包,我們製作差分包需要用到這裡的壓縮包。故每次編譯版本需要將out target product a...