SqlCipher:无法在Android 6.0.1上打开数据库

时间:2019-06-13 20:29:19

标签: android sqlcipher android-6.0.1-marshmallow

以前,我只使用SQLite,从来没有问题,我只是改用SQLCipher,并且在版本7、8、8.1、9的设备上运行良好,仅在Android 6.0.1上,我遇到以下错误:

net.sqlcipher.database.SQLiteException: error code 14: Could not open database
    at net.sqlcipher.database.SQLiteDatabase.dbopen(Native Method)
    at net.sqlcipher.database.SQLiteDatabase.openDatabaseInternal(SQLiteDatabase.java:2575)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1243)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1210)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1182)
    at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1131)
    at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1332)
    at android.xdl.dripapp_opencv.DB.DBSettings.InitializeDatabase(DBSettings.java:41)
    at android.xdl.dripapp_opencv.MainActivity.onCreate(MainActivity.java:109)
    at android.app.Activity.performCreate(Activity.java:6262)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) 
    at android.app.ActivityThread.access$900(ActivityThread.java:150) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:168) 
    at android.app.ActivityThread.main(ActivityThread.java:5885) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

我正在使用以下方法创建数据库,在MainActivity的onCreate方法上调用它:

数据库设置

public void InitializeDatabase(){
    SQLiteDatabase.loadLibs(_ctx);

    String databasePath = _ctx.getDatabasePath(DATABASE_FILE_NAME).getPath();

    SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databasePath, DATABASE_PASS, null);

    database.execSQL(CREATE_PATIENT_RECORDS_TABLE);
    database.execSQL(CREATE_SESSION_RECORDS_TABLE);


    database.close();
}

我对清单有以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我什至试图像其他答案所说的那样在运行时请求该权限,但没有成功,我得到了对话框,在接受该权限后,还会出现相同的错误。

我尝试不使用路径而是使用文件,但出现了相同的错误:

File databaseFile = getDatabasePath(DATABASE_FILE_NAME);
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(databaseFile, DATABASE_PASS, null);

我尝试使用mkdir作为SQLCipher的文档站点,但是什么也没有。

File databaseFile = getDatabasePath(DATABASE_FILE_NAME);
databaseFile.mkdirs();

我正在使用此版本的SQLCipher:

implementation 'net.zetetic:android-database-sqlcipher:4.2.0@aar'

1 个答案:

答案 0 :(得分:0)

在我的6.0.1设备上成功运行所有测试后,进入SqlCipher Tests Project,发现文档中的说法与实现方式不同。

现在起作用的是:

databaseFile.getParentFile().mkdirs();

代替:

databaseFile.mkdirs();