如何在android中选择文件夹?

时间:2011-12-21 09:08:12

标签: android android-layout android-widget

您好有办法选择用户想要在android中保存文件的文件夹。我看了http://code.google.com/p/android-file-dialog/

它具有选择文件的功能,但我想选择文件夹,请提供可用的链接或示例。

4 个答案:

答案 0 :(得分:1)

如何使用OI File Manager?此应用程序具有以下意图:PICK_FILEPICK_DIRECTORY。 页面上甚至还有用于使用Intents的示例代码。

答案 1 :(得分:0)

我在我的应用程序中使用了相同的源代码(非常肯定),并且有一段代码:

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);
    }
}

您只需要编辑它处理if (file.isDirectory())的方式。我建议在Activity中声明一个boolean值,如果该文件是一个目录并且它已经为假,则将其更改为true。然后,如果所述值为true,则遍历目录。此外,当您将所述值更改为true时,您需要致电selectButton.setEnabled(true)。我会说,这比制作自己的代码要复杂得多。

答案 2 :(得分:0)

查看此答案https://stackoverflow.com/a/28479561/779140 我被提到图书馆作者,所以不要犹豫,问任何问题。

答案 3 :(得分:0)

我遇到了同样的问题,最后我使用了NoNonsense-FilePicker

添加到gradle文件

  

compile' com.nononsenseapps:filepicker:4.0.0'

触发文件/文件夹/目录选择

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

处理活动结果以获取所选文件

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


                        }
                    }
                }
            }

        }