Android应用程序中预加载的数据库损坏问题

我有一个
Android应用程序,首先将一个大型数据库下载到asynctask中的sdcard(略高于50mb).下载代码如下.

HttpURLConnection conexion = (HttpURLConnection) url.openConnection();
if(filePath.exists() && filePath.length() > 10000.00)
{
    downloaded = (int) filePath.length();
    conexion.setRequestProperty("Range", "bytes=" + (filePath.length()) + "-");
}
else
    conexion.setRequestProperty("Range", "bytes=" + downloaded + "-");

conexion.setDoInput(true);
conexion.setDoOutput(true);
conexion.setUseCaches(false);
conexion.connect();

try {
    totalFileLength = lengthOfFile + downloaded;
    progressDialog.setMax(lengthOfFile + downloaded);
    // downlod the file
    input = new BufferedInputStream(conexion.getInputStream());
    output = (downloaded==0)? new FileOutputStream(filePath): new FileOutputStream(filePath,true);
    bout = new BufferedOutputStream(output, 1024);

    byte data[] = new byte[1024];

    while ((count = input.read(data, 0, 1024)) >= 0 && running) {
        downloaded += count;
        // publishing the progress....
        //onProgressUpdate((int)(downloaded*100/lengthOfFile));
        progressDialog.setProgress(downloaded);
        bout.write(data, 0, count);
    }
} catch (Exception e) {
    ...
} finally {
    try {
        input.close();
        bout.flush();
        bout.close();
    } catch(IOException e) {
        ...
    }
}

完全下载文件后,我打开数据库并向其中添加一个表.

try {
    myDbHelper.myDataBase.execSQL("CREATE TABLE IF NOT EXISTS FAVORITES (_id integer primary key autoincrement, MushroomID INTEGER)");
    success = true;
} catch (SQLException sqle) {
    throw sqle;
}

现在,这个代码在我运行它的每个设备上运行得非常好,但我的几个用户在execSQL行上得到了这个错误.好像它不是单独使用三星Galaxy SII(没有投诉和随后的堆栈跟踪与任何其他手机的这个错误)但我不完全确定它只是这款手机.

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.daleroy.fungifieldguide/net.daleroy.fungifieldguide.activities.FungiFieldGuide}: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed: CREATE TABLE IF NOT EXISTS FAVORITES (_id integer primary key autoincrement, MushroomID INTEGER)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
       at android.app.ActivityThread.access$1500(ActivityThread.java:117)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:130)
       at android.app.ActivityThread.main(ActivityThread.java:3691)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:507)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
       at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed: CREATE TABLE IF NOT EXISTS FAVORITES (_id integer primary key autoincrement, MushroomID INTEGER)
       at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1904)
       at net.daleroy.fungifieldguide.services.MushroomService.MakeFavoriteTable(MushroomService.java:118)
       at net.daleroy.fungifieldguide.services.MushroomService.OpenDB(MushroomService.java:64)
       at net.daleroy.fungifieldguide.activities.FungiFieldGuide.onCreate(FungiFieldGuide.java:73)
       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

我收到了从具有sgs2的用户下载的数据库副本,并且该文件的校验和已关闭.当我用sqlite manager(firefox插件)打开时,我也看不到文件中的任何数据或数据结构.我让他直接下载数据库的副本并将其放在他的数据文件夹中,应用程序正常工作.所以问题出在下载过程中的某个地方.

最佳答案 从API级别9(Android 2.3)开始,您可以使用
DownloadManager来处理长时间运行的HTTP下载:

The download manager is a system service that handles long-running
HTTP downloads. Clients may request that a URI be downloaded to a
particular destination file. The download manager will conduct the
download in the background, taking care of HTTP interactions and
retrying downloads after failures or across connectivity changes and
system reboots.

以下是演示DownloadManager用法的示例项目:Android DownloadManager Example.

我想你甚至可以尝试将DownloadManager支持到以前的Android版本(如果你需要支持它们).它的源代码可以在这里找到:DownloadManager.java

点赞