open a sqlite database when choosing folder with ACTION_OPEN_DOCUMENT_TREE

寵の児 提交于 2019-12-25 14:09:45

问题


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)

回答1:


I haven't tested, due to Lollipop device not available at this time but this should work, let me know if any issues!

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 gist of ASFUriHelper.getPath() method with intermediate methods can be found here: ASFUriHelper.getPath()




回答2:


Thank you for this answer. The problem for me is that getPath returns NULL on my HTC M8 with SD card when I choose a directory on the SD Card.

That is because the uri does not contain "primary" as espectect in the code of getpath :

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

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

/tree/1AF0-170C:SmartDiveBook/document/1AF0-170C:SmartDiveBook/SmartDiveBook.db 


来源:https://stackoverflow.com/questions/29775068/open-a-sqlite-database-when-choosing-folder-with-action-open-document-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!