How to select a directory in Android API < 21?

Viewed 528

Can anyone tell me what is the best way to let the user choose a directory via the Android file picker in Android with API < 21? Intent.ACTION_OPEN_DOCUMENT_TREE is available since API 21 so can't use it. Do I have to make my own "file picker" or use some library to achieve this?

1 Answers

Ahh sorry use ACTION_GET_CONTENT it would help

Intent Intent = new Intent(Intent.ACTION_GET_CONTENT);
Intent.setType("*/*");
startActivityForResult(Intent, RESULT_CODE);

To retrieve Directory

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

        if(requestCode == Statics.REQUEST_CODE && resultCode == RESULT_OK && data!=null)
        {
            Uri uri = data.getData();
}}

the uri will give you the file's directory as URI.

"There is no platform-defined Intent for choosing a filesystem directory. ACTION_OPEN_DOCUMENT_TREE will let the user choose a document tree on Android 5.1+, but a document tree is not necessarily a filesystem directory, just as ACTION_GET_CONTENT and ACTION_OPEN_DOCUMENT do not necessarily involve files."

How to get the folder path using Intent on Android

Related