传递Intent返回的Uri时,ContentResolver中不受支持的Uri

2019年12月20日 10点热度 0条评论

我有一个应用程序,用户可以通过选择一个文件或将所选目录中的所有文件添加到ListView中来添加音乐文件。当添加单个文件时,一切都很好,但是对于目录,我会得到Unsupported Uri。问题与权限无关:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.dibo.testapp, PID: 12226
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65579, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/tree/raw:/storage/emulated/0/Download/2006 A Good Year flg=0xc3 }} to activity {com.example.dibo.testapp/com.example.dibo.testapp.MainActivity}: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F2006%20A%20Good%20Year
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4360)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.providers.downloads.documents/tree/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F2006%20A%20Good%20Year
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:167)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:418)
        at android.content.ContentResolver.query(ContentResolver.java:802)
        at android.content.ContentResolver.query(ContentResolver.java:752)
        at android.content.ContentResolver.query(ContentResolver.java:710)
        at com.example.dibo.testapp.PlaylistFragment.addFolder(PlaylistFragment.java:380)
        at com.example.dibo.testapp.PlaylistFragment.onActivityResult(PlaylistFragment.java:346)
        at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:160)
        at com.example.dibo.testapp.MainActivity.onActivityResult(MainActivity.java:152)
        at android.app.Activity.dispatchActivityResult(Activity.java:7454)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)


这是意图请求:*

private void selectDirectory() {
    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);

    startActivityForResult(intent, REQUEST_FOLDER);
}


结果处理程序:

@Override
public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {

    if (requestCode == REQUEST_FILE && resultCode == Activity.RESULT_OK) {
        if (resultData != null) {
            addFile(resultData.getData());
        }
    }

    if (requestCode == REQUEST_FOLDER && resultCode == Activity.RESULT_OK) {

        if (resultData != null) {
            addFolder(resultData.getData());
        }
    }
}

private void addFolder(Uri uri) {

    ContentResolver contentResolver = getActivity().getContentResolver();

    if (contentResolver==null) {
        return;
    }

    Cursor cursor = contentResolver.query(
            uri, // Uri
            null,
            null,
            null,
            null
    );

    if (cursor != null &&  cursor.moveToFirst()) {
        int Title = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);

        do {

            String SongTitle = cursor.getString(Title);
            Log.d("addFolder", SongTitle);

        } while (cursor.moveToNext());
    }

    if (cursor != null) {
        cursor.close();
    }

}

contentResolver.query中引发了异常。请注意,所有内容都在
Fragment类中,而不在
Activity中。

解决方案如下:

您首先需要做魔术:

Uri docUriTree = DocumentsContract.buildDocumentUriUsingTree(treeUri, DocumentsContract.getTreeDocumentId(treeUri));

然后,您可以从Uri中读取DocumentsContract.Document值。