Simple Android Directory Collector - How?

I just started coding in Android Studio and I feel amazing .. !!

How to write code for "Directory Picker". that is, when a button is pressed, just a dialog / action screen that can display a list of directories.

In addition, you must save all the files in this directory to an Array variable. (After clicking OK).

PS: I searched here and found some cool "File Select", but I was looking for Directory Chooser ..!

Thanks in advance.

+13
source share
4 answers

Try using Intent.ACTION_OPEN_DOCUMENT_TREE

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 ()

 public void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case 9999: Log.i("Test", "Result URI " + data.getData()); break; } } 
+25
source

You can also use some libraries.
eg:
https://github.com/passy/Android-DirectoryChooser

+4
source

There is an open source library that makes a directory selection and opens / saves file actions as well. It can be found on GitHub at https://github.com/BoardiesITSolutions/FileDirectoryPicker .

Powered by Android API Level 17 and above

Disclaimer: I wrote this

0
source

Use the code below to select a directory

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

And to get the selected path override onActivityResult:

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == PICK_DIRECTORY && resultCode == RESULT_OK) { Bundle extras = data.getExtras(); String path = (String) extras.get("chosenDir"); } } 
-7
source

Source: https://habr.com/ru/post/980864/


All Articles