How to select folder in android?

前端 未结 4 1566
谎友^
谎友^ 2021-01-18 08:31

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 selec

4条回答
  •  礼貌的吻别
    2021-01-18 08:53

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

提交回复
热议问题