android中如何选择文件夹?

发布于 2024-12-22 00:43:59 字数 213 浏览 3 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

聚集的泪 2024-12-29 00:43:59

使用OI 文件管理器怎么样?该应用程序具有以下意图:PICK_FILEPICK_DIRECTORY
页面上甚至还有使用意图的示例代码。

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.

小ぇ时光︴ 2024-12-29 00:43:59

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

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 值,如果文件是一个目录并且它已经是 false,则将其更改为 true 。然后如果该值为true,则遍历该目录。此外,当您将所述值更改为true时,您需要调用selectButton.setEnabled(true)。我想说,这比编写自己的代码要简单得多。

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.

远山浅 2024-12-29 00:43:59

查看此答案https://stackoverflow.com/a/28479561/779140
我被提到是图书馆作者,所以请随时提出任何问题。

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

咽泪装欢 2024-12-29 00:43:59

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

添加到gradle文件

编译“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();


                        }
                    }
                }
            }

        }

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


                        }
                    }
                }
            }

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文