guolindev / LitePal

An Android library that makes developers use SQLite database extremely easy.
Apache License 2.0
8.11k stars 1.59k forks source link

当我设置了存储路径后,启动app报错 #755

Open Traveler1991 opened 3 years ago

Traveler1991 commented 3 years ago

当我设置了存储路径后,app报错日志如下:

2021-09-03 11:10:53.194 25440-25488/org.litepal.litepalsample E/SQLiteLog: (14) cannot open file at line 38589 of [b2325a6e1c] 2021-09-03 11:10:53.194 25440-25488/org.litepal.litepalsample E/SQLiteLog: (14) os_unix.c:38589: (2) open(/storage/emulated/0/.Sample/cache/databases/sample.db) - 2021-09-03 11:10:53.207 25440-25488/org.litepal.litepalsample E/SQLiteDatabase: Failed to open database '/storage/emulated/0/.Sample/cache/databases/sample.db'. android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/storage/emulated/0/.Sample/cache/databases/sample.db': Directory /storage/emulated/0/.Sample/cache/databases doesn't exist at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:257) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210) at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:516) at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206) at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:198) at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:922) at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:902) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:766) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:755) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:378) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:321) at org.litepal.tablemanager.Connector.getWritableDatabase(Connector.java:58) at org.litepal.tablemanager.Connector.getDatabase(Connector.java:70) at org.litepal.litepalsample.activity.SaveSampleActivity$1.run(SaveSampleActivity.java:114) at java.lang.Thread.run(Thread.java:923) Caused b

xiexindev commented 3 years ago

初始化数据库之后, 操作数据库之前必须要先保证你程序拥有外部文件读写权限才能正常创建外部存储路径下的db文件.

db文件创建的时机有下面三种方式, 必须要在执行下面任意方法之前保证程序拥有外部文件读写权限. 否则请不要将数据库保存在外部存储空间:

  1. LitePal.getDatabase()
  2. LitePalSupport的增删改查方法
  3. LitePal的静态增删改查方法