Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsupported Uri in ContentResolver when passing Uri returned by Intent

I have an app where the user can add music files by selecting a file or adding all the files in a selected directory into a ListView. When adding a single file, everything is fine, but for directories I'm getting Unsupported Uri. Problem is not related to permissions:

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)

This is intent request:*

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

    startActivityForResult(intent, REQUEST_FOLDER);
}

Result handler:

@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();
    }

}

Exception is raised in contentResolver.query. Note that everything is in Fragment Class, not in Activity.

like image 791
Dibo Avatar asked Jan 12 '19 17:01

Dibo


1 Answers

You need first do magic thing:

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

Then you can read DocumentsContract.Document values from your Uri.

like image 128
Lnk Avatar answered Oct 06 '22 20:10

Lnk