Skip to content Skip to sidebar Skip to footer

Simple Android Directory Picker - How?

I have just started coding in Android Studio and feeling Awesome..!! How can I write a code for a 'Directory Picker'. i.e., When a button is clicked, a simple Dialog/Activity scre

Solution 1:

Try to use Intent.ACTION_OPEN_DOCUMENT_TREE

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){ 
    Intenti=newIntent(Intent.ACTION_OPEN_DOCUMENT_TREE); 
    i.addCategory(Intent.CATEGORY_DEFAULT);
    startActivityForResult(Intent.createChooser(i, "Choose directory"), 9999);
}

And get the result Uri from onActivityResult data.getData()

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {
        case9999:
            Log.i("Test", "Result URI " + data.getData());
            break;
    }
}

Solution 2:

Also you can use some libraries. for example: https://github.com/passy/Android-DirectoryChooser

Solution 3:

There's an open source library that does directory chooser and open/save file activities as well. It can be found on GitHub at https://github.com/BoardiesITSolutions/FileDirectoryPicker.

Works on Android API Level 17 and above

Disclaimer: I wrote it

Solution 4:

As of Android 10 (API 29), direct access to external storage is deprecated in favor of storage access framework https://developer.android.com/guide/topics/providers/document-provider

Solution 5:

Use below code to select directory

        Intent result=new Intent();
        result.putExtra("chosenDir", path);
        setResult(RESULT_OK, result);

And to get the selected path override onActivityResult :

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == PICK_DIRECTORY && resultCode == RESULT_OK) {
        Bundleextras= data.getExtras();
        Stringpath= (String) extras.get("chosenDir");

    }
}

Post a Comment for "Simple Android Directory Picker - How?"