Simple Android Directory picker - How?

24,787

Solution 1

Try to use Intent.ACTION_OPEN_DOCUMENT_TREE

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){ 
    Intent i = new Intent(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()

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {
        case 9999:
            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

Share:
24,787
Android_Noob
Author by

Android_Noob

An Android Noob..!!! My road to success is still under construction..!!!

Updated on July 28, 2022

Comments

  • Android_Noob
    Android_Noob almost 2 years

    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 screen which can show list of directories.

    Also, want to store all the files in that directory in to an Array variable. (Once OK button is clicked).

    PS: I have searched here and found some cool 'File choose' but m looking for Directory Chooser..!

    Thanks in advance.