How to select folder in android?

16,294

Solution 1

I used the same source in my app (pretty sure), and there is a block of code:

protected void onListItemClick(ListView l, View v, int position, long id) {
    if (file.isDirectory()) {
        selectButton.setEnabled(false);
        if (file.canRead()) {
            lastPositions.put(currentPath, position);
            getDir(path.get(position));
        } else {
            new AlertDialog.Builder(this)
                    .setIcon(R.drawable.icon)
                    .setTitle(
                            "[" + file.getName() + "] "
                                    + getText(R.string.cant_read_folder))
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                        int which) {

                                }
                            }).show();
        }
    } else {
        selectedFile = file;
        v.setSelected(true);
        selectButton.setEnabled(true);
    }
}

You just have to edit how it handle's if (file.isDirectory()). I would recommend declaring a boolean value in your Activity which you change to true if the file is a directory and it is already false. Then if said value is true, then traverse the directory. Also when you change said value to true, you would need to call selectButton.setEnabled(true). This would be quite a bit less complicated than making your own code, I would say.

Solution 2

How about using OI File Manager? This App has the following Intents: PICK_FILE, PICK_DIRECTORY. There is even sample code on the page for using the Intents.

Share:
16,294
Sushant Bhatnagar
Author by

Sushant Bhatnagar

I have the analytical mind necessary to write code and the big-picture mentality to make it innovative and unique. I also enjoy overseeing projects, organising people and channelling them into areas where they can make the best use of their talents. I began my IT career as a front-end developer. My job entailed working with teams of developers to write apps with varying functionalities from mobile to web application. I had experimented with different technologies C#, ASP.NET MVC , Native - Android , Xamarin.Android , Xamarin.iOS , Git , Gitlub, Node.js during this journey. Collaborating with other developers not only honed my coding skills but also taught me the value of how to effectively work with others and subdivide tasks to improve efficiency and ensure smooth teamwork. I later decided to combine my skills towards becoming a DevOps Engineer at Gaussian Networks by adopting cloud - AWS. With me at the helm coordinating the development and IT Operations team, I was able to design CI/CD pipelines that improved the company's ability to design, debug and release software by 20%. Some of the key requirement that I have extensive experience with include: Experience with mobile ui/ux design for multiple devices. In depth knowledge of Google Play/ App Store publishing policies. Good understanding of mobile platform challenges and limitations. Good analytical , logic & critical cases thinking skill Experience with micro service environment setup on Cloud - Containerisation Experience with CI/CD pipelines setup. Lead the project from scratch to production go live. End to end ownership of project delivery.

Updated on July 20, 2022

Comments

  • Sushant Bhatnagar
    Sushant Bhatnagar almost 2 years

    Hi there is a way to select folder where user want to save file in android . I check out http://code.google.com/p/android-file-dialog/

    it has functionality to select file but i want to select folder , please provide me usable link or examples.

  • Sushant Bhatnagar
    Sushant Bhatnagar over 12 years
    if (file.isDirectory()) {selectedFile = file; v.setSelected(true);selectButton.setEnabled(true); if (file.canRead()) { lastPositions.put(currentPath, position); getDir(path.get(position));} else {new AlertDialog.Builder(this) .setIcon(R.drawable.icon).setTitl‌​e( "[" + file.getName() + "] "+ getText(R.string.cant_read_folder)) .setPositiveButton("‌​OK",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }).show();} } else { v.setSelected(false); selectButton.setEnabled(false‌​);}
  • Sushant Bhatnagar
    Sushant Bhatnagar over 12 years
    I have modified function as above , but i want to select folder in place of file , it return previous directory when i traverse directory hierarchy.
  • Marek Sebera
    Marek Sebera over 10 years
    I think question aims for solution integrated, not using external application.
  • Nagabhushan S N
    Nagabhushan S N about 6 years
    Utils.makeHepticFeedback(getActivity()); method not found. If I comment that, I'm getting an Exception java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.app/com.nononsenseapps.filepicker.Fi‌​lePickerActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageButton Any idea?
  • Hitesh Sahu
    Hitesh Sahu about 6 years
    Utils.makeHepticFeedback(getActivity()) is method to make device vibrate and give a button feel it have nothing to do with file picker. You can comment it. It seems like a UI issue stackoverflow.com/questions/35448999/…
  • eri0o
    eri0o over 2 years
    @SushantBhatnagar can you show the modified function for directory?