由openOrCreateDatabase引起的SQLiteDiskIOException

时间:2013-10-21 01:01:53

标签: android

我在开发者控制台中收到了此崩溃报告:

java.lang.RuntimeException: Unable to start activity ComponentInfo{ZhuangDictActivity}: android.database.sqlite.SQLiteDiskIOException: disk I/O error
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.SQLiteDiskIOException: disk I/O error
at android.database.sqlite.SQLiteDatabase.native_setLocale(Native Method)
at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:2144)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1996)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:905)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:995)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:988)
at DatabaseHelper.initDb(DatabaseHelper.java:79)
at ZhuangDictActivity.onCreate(ZhuangDictActivity.java:329)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more

这是造成问题的DabaseHelper.java的一部分:

public void initDb() {
    if (db != null) {
        db.close();
    }
    db = SQLiteDatabase.openOrCreateDatabase(ZhuangDictActivity.dbFile, null);
}

第79行是db = SQLiteDatabase.openOrCreateDatabase(ZhuangDictActivity.dbFile,null) ZhuangDictActiviy.java导致问题的部分:

if (dictName == null) {
} else {
    modifyDictVar(dictName);
    setTitle(loadDictInfo());

    initDbFile();
    databaseHelper.initDb();
}

第329行是databaseHelper.initDb() 抛出IOException的ZhuangDictActivity.java的另一部分,我想:

private void initDbFile() {
    dbFile = new File(dbFileName);
    if (!dbFile.exists()) {
        try {
            dbFile.createNewFile();
        } catch (IOException e) {
            showErrorDialog(getString(R.string.error));
            e.printStackTrace();
        }
    }
}

我的项目涉及将数据库写入外部存储。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

尝试

dbFile = Environment.getDataDirectory() + “/data/YOUR_PACKAGE/databases/”; 

另外,请查看similar answer