gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:53 24 4
gpt4 key购买 nike

我有一个 android 应用程序,它首先在异步任务中将一个大型数据库下载到 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 管理器(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

关于android - Android 应用程序中的预加载数据库损坏问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7577408/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com