I have a sqlite db in SD card. I am trying to open it from an activity but getting the below error.
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)
Below is the code I am using to open DB. I am not able to use Environment.getExternalStorageDirectory()
as this is pointing to internal storage in few phones.
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;
}
}
But the same Db works if it is copied to internal storage.