I have an already existing sqlite database in a folder /sdcard/myfolder/db/mydb.db
. My app shows a dialog to choose a folder:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, 42);
The Dialog pops up and I can choose the folder:

I choose the folder /sdcard/myfolder/db/ and press choose/ok.
My onActivityResult gets called. I go through all files and if the file is my "mydb.db" then I try to open the sqlite database with SQLiteDatabase.openDatabase(); and I get an error (see below)
My guess of the problem: This will not work, because the openDatabase expects the full path to the db like /sdcard/myfolder/db/mydb.db
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // if (requestCode == REQUEST_QUICKLIST) { if (resultCode == Activity.RESULT_OK) { if(requestCode==42){ // TreeDialog for choosing the Installdirectory Uri treeUri = data.getData(); DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri); for (DocumentFile file : pickedDir.listFiles()) { if(file.getName().equals("mydb.db")){ SQLiteDatabase sqliteTest= SQLiteDatabase.openDatabase(file.getUri().getPath(), null, SQLiteDatabase.OPEN_READONLY); } Log.d("DEBUG FILE", "Found file " + file.getName() + " with size " + file.length() + " " + file.getParentFile().getName()); }
file.getUri().getPath()
has the value : /tree/primary:myfolder/db/document/primary:myfolder/db/mydb.db
How to open the sqlite Database ?
Stacktrace:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=42, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/tree/primary:myfolder/db flg=0xc3 }} to activity {mycompany.browser/mycompany.browser.browser}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database at android.app.ActivityThread.deliverResults(ActivityThread.java:3976) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4019) at android.app.ActivityThread.access$1400(ActivityThread.java:172) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1471) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5834) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183) Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:318) at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:228) at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:512) at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:206) at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178) at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:891) at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:861) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:696) at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:671) at mycompany.browser.browser.onActivityResult(browser.java:1533) at android.app.Activity.dispatchActivityResult(Activity.java:6475) at android.app.ActivityThread.deliverResults(ActivityThread.java:3972)