构建我的应用程序的一个步骤是让用户选择存储某些文件的文件夹。如何在不对目录进行硬编码的情况下进行此配置。我可以使用任何第三方库来执行此操作吗?
答案 0 :(得分:7)
@ Vikram的链接提供了一个可以使用的对话框。然后,您可以使用Shared Preferences保存用户选择的目录。
Here是一个关于如何使用共享偏好的简单示例。
可以找到另一个教程here。
更新:我内心突然打开一个开关做这样的事情。对于使用的基本代码,积分仍会转到this answer的schwiz。 :)
//global variables
private File[] fileList;
private String[] filenameList;
private File[] loadFileList(String directory) {
File path = new File(directory);
if(path.exists()) {
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String filename) {
//add some filters here, for now return true to see all files
//File file = new File(dir, filename);
//return filename.contains(".txt") || file.isDirectory();
return true;
}
};
//if null return an empty array instead
File[] list = path.listFiles(filter);
return list == null ? new File[0] : list;
} else {
return new File[0];
}
}
public void showFileListDialog(final String directory, final Context context) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(context);
File[] tempFileList = loadFileList(directory);
//if directory is root, no need to up one directory
if(directory.equals("/")) {
fileList = new File[tempFileList.length];
filenameList = new String[tempFileList.length];
//iterate over tempFileList
for(int i = 0; i < tempFileList.length; i++) {
fileList[i] = tempFileList[i];
filenameList[i] = tempFileList[i].getName();
}
} else {
fileList = new File[tempFileList.length+1];
filenameList = new String[tempFileList.length+1];
//add an "up" option as first item
fileList[0] = new File(upOneDirectory(directory));
filenameList[0] = "..";
//iterate over tempFileList
for(int i = 0; i < tempFileList.length; i++) {
fileList[i+1] = tempFileList[i];
filenameList[i+1] = tempFileList[i].getName();
}
}
builder.setTitle("Choose your file: " + directory);
builder.setItems(filenameList, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
File chosenFile = fileList[which];
if(chosenFile.isDirectory()) {
showFileListDialog(chosenFile.getAbsolutePath(), context);
}
}
});
builder.setNegativeButton("Cancel", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
public String upOneDirectory(String directory) {
String[] dirs = directory.split(File.separator);
StringBuilder stringBuilder = new StringBuilder("");
for(int i = 0; i < dirs.length-1; i++) {
stringBuilder.append(dirs[i]).append(File.separator);
}
return stringBuilder.toString();
}
上面的代码就像一个迷你文件浏览器,列出了Android文件系统的文件和文件夹。你使用它像:
showFileListDialog(Environment.getExternalStorageDirectory().toString(),
MainActivity.this);
回答你的问题: 添加全局关键变量
//package name goes here.
public static final String PACKAGE_NAME = "com.example.app";
public static final String KEY_DIRECTORY_SELECTED =
PACKAGE_NAME + ".DIRECTORY_SELECTED";
private SharedPreferences prefs;
在使用之前在onCreate上的某处初始化您的SharedPreferences:
prefs = getSharedPreferences(PACKAGE_NAME, Context.MODE_PRIVATE);
然后你可以在对话框中添加一个肯定的按钮
builder.setPositiveButton("Save Directory", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
prefs.edit().putString(KEY_DIRECTORY_SELECTED, directory).commit();
}
});
在您的活动中,您可以
而不是使用SDCard默认目录//if no saved directory yet, use SDCard directory as default
String oldChosenDirectory = prefs.getString(KEY_DIRECTORY_SELECTED,
Environment.getExternalStorageDirectory().toString());
showFileListDialog(oldChosenDirectory, MainActivity.this);
维克拉姆指出。您还需要在FileNameFilter中执行此操作,以便对话框仅显示目录。
更新:如this SO answer所述,参数dir
和filename
未引用同一文件。 dir
参数是包含文件的目录,而filename
是文件本身的文件名。要确定过滤器本身是否是目录,我们需要从参数中创建一个新文件,如下所示:
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String filename) {
File file = new File(dir.getAbsolutePath() + File.separator + filename);
return file.isDirectory();
}
};