如何在安卓系统中选择文件夹?

8

你好,有一种方法可以让用户在Android上选择文件保存的文件夹。我查看了http://code.google.com/p/android-file-dialog/

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


你可以通过编程创建目录。 - Nikunj Patel
我建议您创建自己的文件/文件夹选择器,并使用意图调用它。这有点繁琐,但您可以控制自己的代码,并且可以从未来的应用程序中调用它。否则,请寻找具有公共API的第三方文件浏览器,通过意图进行调用。 - Mister Smith
很遗憾这个功能没有内置。Android已经有多少个版本了,他们的开发者都没有想到人们会想要选择文件夹。
  • 我非常困惑 Ted :-)
- Paul McCarthy
4个回答

1

使用OI文件管理器如何?此应用程序具有以下意图:PICK_FILEPICK_DIRECTORY。页面上甚至有使用这些意图的示例代码。


5
我认为这个问题的目的是为了集成解决方案,而不是使用外部应用程序。 - Marek Sebera

0

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 中声明一个布尔值,如果文件是目录并且还是 false,就将其更改为 true。然后,如果该值为 true,则遍历目录。同样,在将该值更改为 true 时,您需要调用 selectButton.setEnabled(true)。我认为这比编写自己的代码要简单得多。


如果(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
1
我已经按照上述修改了函数,但我想选择文件夹而不是文件,当我遍历目录层次结构时,它返回先前的目录。 - Sushant Bhatnagar
@SushantBhatnagar,你能展示修改后的目录函数吗? - eri0o

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

处理Activity结果以获取所选文件或文件

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


                        }
                    }
                }
            }

        }

Utils.makeHepticFeedback(getActivity()); 方法未找到。如果我注释掉它,我会得到一个异常 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 有什么想法吗? - Nagabhushan S N
2
Utils.makeHepticFeedback(getActivity()) 是一种使设备振动并给予按钮感觉的方法,与文件选择器无关。您可以对其进行评论。这似乎是一个UI问题。https://dev59.com/_ZTfa4cB1Zd3GeqPMyhH - Hitesh Sahu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接