以前我只使用 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();
}
我对 Manifest 拥有以下权限:
<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'