Open sqlite database when selecting folder with ACTION_OPEN_DOCUMENT_TREE

I have an existing sqlite database in the /sdcard/myfolder/db/mydb.db folder. My application displays a dialog box for selecting a folder:

 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); startActivityForResult(intent, 42); 

A dialog appears and I can select the folder:

enter image description here

I select the folder / sdcard / myfolder / db / and press the select button / ok.

Called My onActivityResult. I look through all the files, and if the file is my "mydb.db", I try to open the sqlite database using SQLiteDatabase.openDatabase (); and I get an error (see below)

My assumption about the problem: This will not work because openDatabase expects the full path to db, for example /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() matters: /tree/primary:myfolder/db/document/primary:myfolder/db/mydb.db

How to open 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) 
0
android android-intent android-5.0-lollipop sqlite
Apr 21 '15 at 14:25
source share
3 answers

I did not test, because the Lollipop device is not available at this time, but it should work, let me know if there are any problems!

 if(file.getName().equals("mydb.db")){ Uri uri = file.getUri(); Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri)); String path = ASFUriHelper.getPath(this, docUri); SQLiteDatabase sqliteTest= SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); } 

The essence of the ASFUriHelper.getPath () method with intermediate methods can be found here: ASFUriHelper.getPath ()

+1
Apr 22 '15 at 16:39
source share

Thanks for this answer. The problem for me is that getPath returns NULL on my HTC M8 with an SD card when I select a directory on the SD card.

This is because uri does not contain "primary" like espectect in getpath code:

 if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } 

for me, the type contains "1AF0-170C", and the full uri:

 /tree/1AF0-170C:SmartDiveBook/document/1AF0-170C:SmartDiveBook/SmartDiveBook.db 
+1
Jun 26 '15 at 8:27
source share

You tried to remove the statement: if ("primary" .equalsIgnoreCase (type)) and check if it works?

0
Aug 07 '15 at 8:11
source share



All Articles