0

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
kahlflekzy
  • 28
  • 8
  • You will need to copy the database to some file that you control (e.g., in `getFilesDir()`). You can use `ContentResolver` and `openInputStream()` to get an `InputStream` on the content identified by the `Uri`. Then, use that `InputStream` as the source of data to copy to some `FileOutputStream`. You can then use the file with `SQLiteDatabase`, Room, etc. – CommonsWare Dec 24 '19 at 13:59
  • Thanks. There are some columns and/or rows that i'll like to remove/add from the main database, so i'll ideally like to create a new db then write the new rows/columns. I'm thinking simply copying the db isn't what i want. – kahlflekzy Dec 24 '19 at 14:03
  • Copying it *is* creating a new database. You would then use `ALTER TABLE` statements to add your columns, along with insert operations to add rows. – CommonsWare Dec 24 '19 at 14:05
  • Awesome, lemme just try that out. I'll get back to you ASAP! – kahlflekzy Dec 24 '19 at 14:13
  • Thanks for your inputs, @commonsware. I adjusted the previous implementation. I created a new db, using the normal way of creating dBs, (based on your recommendation of creating one in a dir I had control of) then I populated it with values from the main db, then got a File handle to it, Then copied its content to the Uri using the content resolver techniques you suggested. Then deleted the new db. When I copied the Uri file and check its content in a PC database tool, it shows the desired content. Thanks a lot once more. – kahlflekzy Dec 25 '19 at 14:46

0 Answers0