Unable to create OPEN_DOCUMENT_TREE activity under Android 12

1.3k views Asked by At

I'm attempting to open a system file dialog, to allow the user to select a location to store application configuration XML files. I am trying to use the ACTION_OPEN_DOCUMENT_TREE intent to initiate the dialog, but the application crashes instead, and logcat is showing the error:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.OPEN_DOCUMENT_TREE cat=[android.intent.category.OPENABLE]

The error is generated when running the app on the emulator with API version 32. I have tried setting flags on the intent (read/write), but that doesn't make any difference.

Any idea what could be causing this?

Here's my code:

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private Uri ProfileFolderUri;
    private static final int requestCode = 9479;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding;

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.toolbar);

        /** TODO replace below static string with Uri from preferences */
        this.ProfileFolderUri = Uri.parse("content://com.android.externalstorage.documents/tree/");

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

    }


    @Override
    protected void onResume() {
        super.onResume();

        if ( !hasWriteAccessForUri( this, this.ProfileFolderUri ) ) {
            getProfilesFolder();
        }
    }


    private void getProfilesFolder() {
        Intent intent = new Intent( Intent.ACTION_OPEN_DOCUMENT_TREE );
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult( intent, requestCode );
    }


    @Override
    protected void onActivityResult(int reqCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(reqCode, resultCode, data);
        if ( resultCode == RESULT_OK && reqCode == requestCode ) {
            if (data != null) {
                Uri treeUri = data.getData();
                String text = treeUri.toString();
                if ( !text.equals("") ) {
                    ContentResolver contentResolver = this.getContentResolver();
                    contentResolver.takePersistableUriPermission(
                            treeUri,
                            Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
                                    Intent.FLAG_GRANT_READ_URI_PERMISSION
                    );
                    /** TODO store selected URI to preferences */
                }
            }
        }
    }

}
1

There are 1 answers

0
Jan Petersen On

Issue was with the openable filter. Changing the method to:

private void getProfilesFolder() {
    Intent intent = new Intent( Intent.ACTION_OPEN_DOCUMENT_TREE );
    startActivityForResult( intent, requestCode );
}

Fixed the issue.