i am actually creating this question from Here, since i couldnt comment there. I used this answer to try some things but the implementation relied on building a file path from scatch. I'll like to know if there is a way and how to build the database from the Uri returned by ACTION_CREATE_DOCUMENT
So far, i've been able to get the Uri from onActivityResult, but passing it the database creator raises an error.
Below are my code samples.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CREATE_FILE && resultCode == Activity.RESULT_OK){
uri = data?.data
val backupTask = BackupTask(this, null, uri!!.path, getFileName(uri!!))
backupTask.execute()
}
super.onActivityResult(requestCode, resultCode, data)
}
BackUpTask.doInBackground {
//i've eliminated unneccessary AsyncTask codes here
val customContext = DBContext(filePath, contextWeakReference.get())
val dbh = DBH1(customContext, dbFileName)
}
class DBContext(private val dbFilePath: String, base: Context?) : ContextWrapper(base){
override fun getDatabasePath(name: String?): File {
// **is there a way i can use the uri here?? instead of name?**
val result = File(name)
return result
}
override fun openOrCreateDatabase(name: String?, mode: Int, factory: SQLiteDatabase.CursorFactory?, errorHandler: DatabaseErrorHandler?): SQLiteDatabase {
return openOrCreateDatabase(name, mode, factory)
}
override fun openOrCreateDatabase(name: String?, mode: Int, factory: SQLiteDatabase.CursorFactory?): SQLiteDatabase {
val result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(dbFilePath), null)
return result
}
}
Running the app ultimately crashes when i try creating/accessing the db
Failed to open database '/document/home:file_backup.db (1)'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database