8

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.

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Sushant Bhatnagar
  • 3,684
  • 10
  • 31
  • 42
  • you can create dir programatically – Nikunj Patel Dec 21 '11 at 09:10
  • I suggest you to create your own file/folder picker and call it with intents. It is a bit tedious but you are in control of your code and you can call it from you future applications. Otherwise look for a 3rd party filexplorer that has a public API via Intents. – Mister Smith Dec 21 '11 at 09:37
  • Sad that this isn't built in. How many versions of Android have there been and none of their developers thought people would want to select a folder - I'm hugely confused Ted :-) – Paul McCarthy Dec 12 '22 at 23:52

4 Answers4

1

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.

seeker
  • 671
  • 7
  • 13
0

Check out this answer https://stackoverflow.com/a/28479561/779140 I am mentioned library author so don't hesitate to ask any questions.

Community
  • 1
  • 1
Ruslan Yanchyshyn
  • 2,774
  • 1
  • 24
  • 22
0

I encountered the same issue and I end up using NoNonsense-FilePicker

Add to gradle file

compile 'com.nononsenseapps:filepicker:4.0.0'

Trigger file/folder/dir pick

try {

                    Utils.makeHepticFeedback(getActivity());

                    Intent selectDirectoyIntent = new Intent(getActivity(), FilePickerActivity.class);
                    selectDirectoyIntent.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
                    selectDirectoyIntent.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, true);
                    selectDirectoyIntent.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_DIR);
                    selectDirectoyIntent.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());
                    startActivityForResult(selectDirectoyIntent, FILE_CODE);

                } catch (Exception e) {
                    Log.e(LOG_TAG, "exception", e);
                    e.printStackTrace();

                    Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show();
                }   

Handle Activity result to get selected file or files

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

            if (resultCode == Activity.RESULT_OK && requestCode == CHOOSE_IMAGE_REQUEST_CODE) {
                Uri selectedImageUri = data.getData();
                String selectedImagePath = getRealPathFromURI(selectedImageUri);


                // NOW WE HAVE OUR WANTED STRING
                if (selectedImagePath != null) {
                    System.out
                            .println("selectedImagePath is the right one for you!");

                    PreferenceHelper.getPreferenceHelperInstance().setString(getActivity(),
                            PreferenceHelper.PLAYER_BACKGROUND,
                            selectedImageUri.toString());

                    Glide.with(getActivity()).load(Uri.parse(
                            PreferenceHelper.getPreferenceHelperInstance().getString(getActivity(),
                                    PreferenceHelper.PLAYER_BACKGROUND
                                    , AppConstants.DEFAULT_BACKGROUND_URL))).
                            into((ImageView) ButterKnife.findById(getActivity(), R.id.play_back));

                }
            } else if (requestCode == FILE_CODE && resultCode == Activity.RESULT_OK) {

                if (null != data && !data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) {
                    // The URI will now be something like content://PACKAGE-NAME/root/path/to/file
                    Uri uri = data.getData();
                    // A utility method is provided to transform the URI to a File object
                    File file = com.nononsenseapps.filepicker.Utils.getFileForUri(uri);
                    // If you want a URI which matches the old return value, you can do
                    Uri fileUri = Uri.fromFile(file);
                    // Do something with the result...

                    Snackbar.make(fileFormat, "Recording folder updated to" + fileUri.getPath() + " ¯\\_(ツ)_/¯ ", Snackbar.LENGTH_SHORT).show();

                    AppConfig.RECORDING_FOLDER = fileUri.getPath();

                    PreferenceHelper.getPreferenceHelperInstance().setString(getActivity(), PreferenceHelper.RECORDING_FOLDER, AppConfig.RECORDING_FOLDER);

                    setUpSettingValue();

                } else {

                    // Handling multiple results is one extra step
                    ArrayList<String> paths = data.getStringArrayListExtra(FilePickerActivity.EXTRA_PATHS);
                    if (paths != null) {
                        for (String path : paths) {
                            Uri uri = Uri.parse(path);
                            // Do something with the URI
                            File file = com.nononsenseapps.filepicker.Utils.getFileForUri(uri);
                            // If you want a URI which matches the old return value, you can do
                            Uri fileUri = Uri.fromFile(file);
                            // Do something with the result...

                            Toast.makeText(getActivity(), "Selected dir" + fileUri.getPath(), Toast.LENGTH_SHORT).show();


                        }
                    }
                }
            }

        }
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • `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.FilePickerActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class ImageButton` Any idea? – Nagabhushan S N Feb 14 '18 at 15:42
  • 2
    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 https://stackoverflow.com/questions/35448999/error-inflating-class-imagebutton – Hitesh Sahu Feb 22 '18 at 04:55
0

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.

Reed
  • 14,703
  • 8
  • 66
  • 110
  • 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).setTitle( "[" + 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 Dec 21 '11 at 12:29
  • 1
    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. – Sushant Bhatnagar Dec 21 '11 at 12:31
  • @SushantBhatnagar can you show the modified function for directory? – eri0o Jan 08 '22 at 22:56