【问题标题】:How to select folder in android?如何在android中选择文件夹?
【发布时间】:2012-01-25 02:49:25
【问题描述】:

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

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

【问题讨论】:

  • 您可以通过编程方式创建目录
  • 我建议您创建自己的文件/文件夹选择器并使用意图调用它。这有点乏味,但是您可以控制自己的代码,并且可以从将来的应用程序中调用它。否则,通过 Intents 寻找具有公共 API 的第 3 方文件浏览器。

标签: android directory android-widget picker filechooser


【解决方案1】:

使用OI File Manager怎么样?这个应用程序有以下意图:PICK_FILEPICK_DIRECTORY。 页面上甚至还有使用 Intents 的示例代码。

【讨论】:

  • 我认为问题旨在集成解决方案,而不是使用外部应用程序。
【解决方案2】:

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

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()) 的方式。我建议在你的活动中声明一个boolean 值,如果文件是一个目录并且它已经是错误的,你将其更改为true。然后如果所述值为真,则遍历目录。此外,当您将所述值更改为true 时,您需要调用selectButton.setEnabled(true)。我会说,这比编写自己的代码要简单得多。

【讨论】:

  • 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);}
  • 我已经修改了上面的函数,但是我想选择文件夹代替文件,当我遍历目录层次结构时它会返回上一个目录。
  • @SushantBhatnagar 你能显示修改后的目录功能吗?
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    我遇到了同样的问题,我最终使用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();
    
    
                            }
                        }
                    }
                }
    
            }
    

    【讨论】:

    • 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 任何想法?
    • Utils.makeHepticFeedback(getActivity()) 是一种使设备振动并让按钮感觉与文件选择器无关的方法。你可以评论它。这似乎是一个 UI 问题stackoverflow.com/questions/35448999/…
    猜你喜欢
    • 1970-01-01
    • 2011-06-15
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-24
    • 2017-10-21
    • 1970-01-01
    • 2021-11-11
    相关资源
    最近更新 更多