嗨有一种方法来选择用户想要在android中保存文件的文件夹。我检查了http://code.google.com/p/android-file-dialog/如何在android中选择文件夹?
它具有以下功能:选择文件,但我要选择的文件夹,请给我提供有用的链接或例子。
嗨有一种方法来选择用户想要在android中保存文件的文件夹。我检查了http://code.google.com/p/android-file-dialog/如何在android中选择文件夹?
它具有以下功能:选择文件,但我要选择的文件夹,请给我提供有用的链接或例子。
我用同样的源在我的应用程序(很肯定),并有一个代码块:
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
值,如果该文件是一个目录并且它已经是false,则更改为true
。然后如果所述值为真,则遍历该目录。此外,当您将所述值更改为true
时,您需要致电selectButton.setEnabled(true)
。这会比制作自己的代码复杂得多,我会说。
请为我提供修改后的代码。 – 2011-12-21 12:05:19
if(file.isDirectory()){selectedFile = file; \t \t v.setSelected(true); selectButton.setEnabled(true); (file.canRead()){\t \t \t lastPositions.put(currentPath,position); GETDIR(path.get(位置));}其他{新AlertDialog.Builder(本)\t \t .setIcon(R.drawable.icon).setTitle(\t \t \t \t \t “[” + file.getName()+ “]”+ getText(R.string.cant_read_folder))\t \t \t \t \t .setPositiveButton(“OK”,new DialogInterface。OnClickListener(){ 公共无效的onClick(DialogInterface对话框,\t \t \t \t \t \t \t \t \t \t INT其中){\t \t \t \t \t \t \t \t} \t \t \t \t \t \t \t \t})表示(); }} 其他{\t \t \t \t \t \t v.setSelected(false); \t \t \t selectButton.setEnabled(假);} – 2011-12-21 12:29:22
我已经修改功能同上,但是我要选择文件夹中的文件发生的,它返回以前的目录时,我遍历目录层次结构。 – 2011-12-21 12:31:00
如何使用OI File Manager?这个应用程序有以下意图:PICK_FILE
,PICK_DIRECTORY
。 在页面上甚至有使用Intents的示例代码。
我认为问题旨在解决方案集成,而不是使用外部应用程序。 – 2013-08-04 18:25:39
看看这个答案https://stackoverflow.com/a/28479561/779140 我被提到图书馆的作者,所以不要犹豫,问任何问题。
我遇到了同样的问题,我最终使用NoNonsense-FilePicker
添加到摇篮文件
编译 '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();
}
Handle Act ivity结果得到选定的文件或文件
@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:无法启动活动ComponentInfo {} com.myapp.app/com.nononsenseapps.filepicker.FilePickerActivity:android.view.InflateException:二进制XML文件中的行#0:二进制XML文件行#0:错误充气类ImageButton' 任何想法? – 2018-02-14 15:42:16
Utils.makeHepticFeedback(getActivity())是方法,使设备振动,给一个按钮觉得它什么都没有做与文件选择器。你可以评论它。这似乎是一个UI问题https://stackoverflow.com/questions/35448999/error-inflating-class-imagebutton – 2018-02-22 04:55:38
您可以创建目录编程 – 2011-12-21 09:10:56
我建议你创建自己的文件/文件夹选择器,并意图调用它。它有点乏味,但是你控制着你的代码,你可以从你的未来应用程序中调用它。否则,请通过Intents寻找具有公共API的第三方filexplorer。 – 2011-12-21 09:37:58