我有一個存儲在apk資源中的20MB數據庫,它在第一次運行時被提取使用。要做到這一點,我用我的時候不使用ProGuard從日食(Android 2.2的目標)出口使用proguard時從資源解壓縮不起作用
PackageManager pm = context.getPackageManager();
String apkFile = pm.getApplicationInfo(context.getPackageName(), 0).sourceDir;
ZipFile zipFile = new ZipFile(apkFile);
ZipEntry entry = zipFile.getEntry("assets/FILENAME");
myInput = zipFile.getInputStream(entry);
myOutput = new FileOutputStream(file);
byte[] buffer = new byte[1024*4];
int length;
int total = 0;
int counter = 1;
while ((length = myInput.read(buffer)) > 0) {
total += length;
counter++;
if (counter % 32 == 0) {
publishProgress(total);
}
myOutput.write(buffer, 0, length);
}
一切工作正常。當我用proguard導出時,解壓縮開始工作幾秒鐘(並且一些進度更新,到8%),但是隨後在java.util.zip.InflaterInputStream.read()中崩潰。 )
它適用於模擬器,但在設備上崩潰(很多設備,但我認爲總是在Android 4中工作,在Android 2.2中崩潰)。我的proguard.cfg基本上是默認的。 沒有我試過改變似乎有幫助,任何想法?
爲什麼不使用'getResources()。getAssets()'獲取'AssetManager',並使用它來獲取您的資產? – CommonsWare 2012-03-24 13:05:27
我相信只限於文件少於1MB,至少在Android 2上,而直接解壓縮的作品(至少沒有proguard)。 – wordy 2012-03-24 19:29:49
'AssetManager'適用於1 MB文件。但是,他們必須有一個文件擴展名,以防止它們被壓縮。例如,對於1 MB文件,['SQLiteAssetHelper'](https://github.com/jgilfelt/android-sqlite-asset-helper)可以很好地工作,通過簡單的方法將它們存儲在'資產/'項目文件夾。 – CommonsWare 2012-03-24 19:39:00