从SD卡访问Sqlite DB会出错
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从SD卡访问Sqlite DB会出错相关的知识,希望对你有一定的参考价值。
我在SD卡中有一个sqlite数据库。我试图从活动中打开它,但得到以下错误。
Failed to open database '/storage/sdcard1/deltalearn/deltalearn.db'.
android.database.sqlite.SQLiteException: not an error (code 0): Could not open the database in read/write mode.
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:214)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:198)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
下面是我用来打开DB的代码。我无法使用Environment.getExternalStorageDirectory()
,因为它指向少数手机中的内部存储。
DatabaseHandler databaseHandler = new DatabaseHandler(this);
public DatabaseHandler(Context context) {
super(context, AndroidUtils.getExternalStoragePath()
+ File.separator + Constants.DATABASE_FOLDER_NAME
+ File.separator + DATABASE_NAME, null, DATABASE_VERSION);
Log.d("DatabaseHandler::","public DatabaseHandler(Context context)");
File dbFile=new File(AndroidUtils.getExternalStoragePath()
+ File.separator + Constants.DATABASE_FOLDER_NAME
+ File.separator + DATABASE_NAME);
SQLiteDatabase.openOrCreateDatabase(dbFile, null);
Log.d("DatabaseHandler::", "path: route:" + AndroidUtils.getExternalStoragePath()
+ File.separator + Constants.DATABASE_FOLDER_NAME
+ File.separator + DATABASE_NAME);
}
public static String getExternalStoragePath() {
String removableStoragePath = "";
//working in Moto, but not working in Samsung S3
File fileList[] = new File("/storage/").listFiles();
for (File file : fileList) {
if (!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead()) {
removableStoragePath = file.getAbsolutePath();
}
}
Log.d("AndroidUtils:: ", "getExternalStoragePath: removableStoragePath:" + removableStoragePath);
if (removableStoragePath.contains("emulated")) {
//working in Samsung s3, but not working in Moto
String path = "";
File file = new File("/system/etc/vold.fstab");
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (fr != null) {
br = new BufferedReader(fr);
String s = br.readLine();
while (s != null) {
if (s.startsWith("dev_mount")) {
String[] tokens = s.split("\s");
path = tokens[2]; //mount_point
if (!Environment.getExternalStorageDirectory().getAbsolutePath().equals(path)) {
break;
}
}
s = br.readLine();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
if (br != null) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Log.d("AndroidUtils::", "getExternalStoragePath: path:" + path);
return path;
} else {
//working in Moto, but not working in Samsung galaxy S3
return removableStoragePath;
}
}
但是如果它被复制到内部存储器,则相同的Db可以工作。
答案
您应该验证您使用getWriteableDatabase()
。
此外,qsqxswpoi并不总是指向SD卡,也可能指向内部存储器,因设备而异,因此您无法真正依赖它。据我所知,没有一种方法或方法可以随时获得SD卡。这可能就是为什么它不适用于某些设备。为什么不将数据库存储在内部存储中?只有您的应用可以访问它的位置。如果要将图像保存为blob,则可以改为存储它们的路径。
以上是关于从SD卡访问Sqlite DB会出错的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向使用 DB Browser 查看并修改 SQLite 数据库 ( 从 Android 应用数据目录中拷贝数据库文件 | 使用 DB Browser 工具查看数据块文件 )(代码片段