我正在尝试从应用程序的assets文件夹中复制数据库。代码在Android 2.1 / 2.3上完美运行,但是当我在Android 2.2设备上执行相同的代码时,它会引发异常:
09-03 15:54:09.762: I/System.out(610): File Created successfully
09-03 15:54:09.762: D/asset(610): Data exceeds UNCOMPRESS_DATA_MAX (4640768 vs 1048576)
09-03 15:54:09.762: W/System.err(610): java.io.IOException
09-03 15:54:09.782: W/System.err(610): at android.content.res.AssetManager.readAsset(Native Method)
09-03 15:54:09.782: W/System.err(610): at android.content.res.AssetManager.access$700(AssetManager.java:36)
09-03 15:54:09.862: D/dalvikvm(610): GC_FOR_MALLOC freed 2198 objects / 260688 bytes in 71ms
09-03 15:54:09.862: W/System.err(610): at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
09-03 15:54:09.862: W/System.err(610): at com.emobi.metro.Help.createDatabase(Help.java:39)
09-03 15:54:09.862: W/System.err(610): at com.emobi.metro.Help.<init>(Help.java:22)
09-03 15:54:09.862: W/System.err(610): at com.emobi.metro.Fair.onCreate(Fair.java:41)
09-03 15:54:09.862: W/System.err(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-03 15:54:09.862: W/System.err(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-03 15:54:09.862: W/System.err(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-03 15:54:09.862: W/System.err(610): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-03 15:54:09.862: W/System.err(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-03 15:54:09.862: W/System.err(610): at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 15:54:09.862: W/System.err(610): at android.os.Looper.loop(Looper.java:123)
09-03 15:54:09.862: W/System.err(610): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-03 15:54:09.862: W/System.err(610): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:54:09.862: W/System.err(610): at java.lang.reflect.Method.invoke(Method.java:521)
09-03 15:54:09.862: W/System.err(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-03 15:54:09.862: W/System.err(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-03 15:54:09.862: W/System.err(610): at dalvik.system.NativeStart.main(Native Method)
以下功能用于从资产中复制数据库:
private void createDatabase() {
File DbFile = new File(
"data/data/com.emobi.metro/databases/mymetro");
if (DbFile.exists()) {
System.out.println("file already exist ,No need to Create");
} else {
try {
DbFile.createNewFile();
System.out.println("File Created successfully");
InputStream is = context.getAssets().open(databasename);
FileOutputStream fos = new FileOutputStream(DbFile);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) > 0) {
fos.write(buffer, 0, length);
}
System.out.println("File succesfully placed on sdcard");
// Close the streams
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
是的我也试过了,但后来我得到了Exception
09-03 16:14:26.652: E/AndroidRuntime(2165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emobi.metro/com.emobi.metro.Fair}: android.database.sqlite.SQLiteException: no such table: fair: , while compiling: select stations from fair
数据库中有哪些。
答案 0 :(得分:0)
在网上有很多可能的解决方案,这里有关于stackoverflow的一个很好的讨论:I get this error: Data exceeds UNCOMPRESS_DATA_MAX on android 2.2 but not on 2.3
似乎与android压缩1 mb以上的文件有关。
答案 1 :(得分:0)
您的数据超过1 MB以上 在这种情况下,只需将数据库名称重命名为dbname.mp3
private void copyDataBase() throws IOException {
try {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open("databases/" + DB_NAME + ".mp3");
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("Error in copy DB", e.toString());
}
}
答案 2 :(得分:0)
使用winzip真正压缩数据库并将zip放入资产文件夹,在运行时解压缩并将其复制到数据库文件夹。