Android 4.4及以上版本如何在SD卡上的SQLite数据库中插入记录

Android 4.4 and above version how to insert record in SQLite database on SD card

我必须读取和写入 SD 卡上的 sqlite 数据库(不在内部存储中)。我熟悉 sqlite 模式:

database = SQLiteDatabase.openDatabase("mnt/sdcard2/test/temp/abcd.db",null,
            SQLiteDatabase.OPEN_READWRITE);

而且我确定 "abcd.db" 存在于 sdcard 上。

但我收到以下错误:

cannot open file at line 30217 of [00bb9c9ce4] Failed to open database '/mnt/sdcard2/test/temp/abcd.db' android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

此代码也适用于 android 4.2.* 及以下版本。 我在使用 android 4.4 及更高版本时遇到了这个问题。

请帮助我。

谢谢。

从 Android 4.4 开始,您无法在应用程序包目录之外自由访问 SD 卡。按照 https://source.android.com/devices/storage/index.html 的说明,根据给定的示例更改 SD 卡上的路径

For example, the app with package name com.example.foo can now freely access Android/data/com.example.foo/ on external storage devices with no permissions.

如果您真的想访问其他目录,请使用存储访问框架https://developer.android.com/guide/topics/providers/document-provider.html