Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Android Directory picker - How?

Tags:

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.

like image 491
Android_Noob Avatar asked Jan 12 '15 09:01

Android_Noob


People also ask

How do I select a folder in Android?

To pick folderIntent intent = new Intent(this, FolderPicker. class); startActivityForResult(intent, FOLDERPICKER_CODE); If the user selects folder/file, the name of folder/file will be returned to you on onActivityResult method with the variable name 'data'.

How do I get a list of images in a specific folder on Android?

You can use below code to get all images from specific folder. 1) First you need to define File object to get the storage and appened the name of the folder you want to read. File folder = new File(Environment. getExternalStorageDirectory().

How do I get a list of audio files in a specific folder on Android?

id. mylist); myList = new ArrayList<String>(); File directory = Environment. getExternalStorageDirectory(); file = new File( directory + "/Test" ); File list[] = file. listFiles(); for( int i=0; i< list.


1 Answers

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;     } } 
like image 63
Milton Avatar answered Sep 27 '22 21:09

Milton