Pfredd
Pfredd

Reputation: 487

Why does my app issue Unsupported URI content error?

I am attempting to write an Android app that needs to query the user to select a directory where some data files are located.

When the following code is executed, the Android file picker is launched and I select a directory.

The URI returned looks valid to me:

content://com.android.externalstorage.documents/tree/primary%3ANotifications

Then, when I query the content provider, it crashes.

The error returned is this:

 Caused by: java.lang.UnsupportedOperationException: Unsupported Uri content://com.android.externalstorage.documents/tree/primary%3ANotifications

Here is my code:

package com.muddco.fptest2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    static String TAG = "TEST123";
    public static TextView textView, uriView;
    public  Object act = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.view1);
        uriView = (TextView) findViewById(R.id.uriview);;

        showFileChooser();
    }

    private static final int FILE_SELECT_CODE = 0;

    private void showFileChooser() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a Directory"),
                    FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            // Potentially direct the user to the Market with a Dialog
            Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FILE_SELECT_CODE:
                if (resultCode == RESULT_OK) {
                    // Get the Uri of the selected file
                    Uri uri = data.getData();
                    Log.d(TAG, "File Uri: " + uri.toString());
                    uriView.setText("URI: "+ uri.toString());

                    final int takeFlags = data.getFlags()
                            & (Intent.FLAG_GRANT_READ_URI_PERMISSION
                            | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                    getContentResolver().takePersistableUriPermission(uri, takeFlags);
                    ContentResolver contentResolver = getContentResolver();
                    String[] projection = { "*" };

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

                    // We never get here - the query crashes with an Unsuported URI content error

                    if(cursor!=null) {

                        TextView pathView = (TextView) findViewById(R.id.pathView);
                        pathView.setText("URI: ");

                        cursor.moveToFirst();

                        // Loop in the cursor to get each row.
                        do {
                            // Get column 1 value.
                            int column1Index = cursor.getColumnIndex("column1");
                            String column1Value = cursor.getString(column1Index);

                            // Get column 2 value.
                            int column2Index = cursor.getColumnIndex("column2");
                            String column2Value = cursor.getString(column2Index);

                            pathView.setText(pathView.getText() + column1Value + " | " + column2Value + "\n");

                        } while (cursor.moveToNext());
                    }


                    //Toast.makeText(this, "File Uri: " + uri.toString(),
                    //        Toast.LENGTH_LONG).show();
                    // Get the path
                    // Get the file instance
                    // File file = new File(path);
                    // Initiate the upload
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}

I have set permissions in the manifest with:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Please excuse my poor coding, as I am just learning Android programming.

Any suggestions on how to make thw work correctly would be greatly appreciated.

Thanks!!!

Upvotes: 6

Views: 4700

Answers (1)

Pfredd
Pfredd

Reputation: 487

Thanks to use CommonsWare, I have managed to get things working.

Here is the working code:

package com.muddco.fl99;

import android.content.Context;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;;
import androidx.documentfile.provider.DocumentFile;


public class MainActivity extends AppCompatActivity {

    static String TAG = "TEST123";
    public static TextView fileView, uriView;
    public Context act = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fileView = (TextView) findViewById(R.id.fileView);
        uriView = (TextView) findViewById(R.id.uriView);;

        showFileChooser();
    }

    private static final int FILE_SELECT_CODE = 0;

    private void showFileChooser() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a Directory"),
                    FILE_SELECT_CODE);
        } catch (android.content.ActivityNotFoundException ex) {
            // Potentially direct the user to the Market with a Dialog
            Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case FILE_SELECT_CODE:
                if (resultCode == RESULT_OK) {
                    // Get the Uri of the selected file
                    Uri uri = data.getData();
                    Log.d(TAG, "File Uri: " + uri.toString());
                    uriView.setText("URI: "+ uri.toString());

                    DocumentFile dfile = DocumentFile.fromTreeUri(act, uri);
                    DocumentFile[] fileList = dfile.listFiles();
                    Log.d(TAG,fileList.toString() );
                    int jj=1;

                    for (DocumentFile docfile : fileList) {

                        Log.d(TAG, "File: " + docfile.getUri() + "\n");
                    }
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }


}

Upvotes: 3

Related Questions