Android:提供内部文件选择器作为选择

时间:2016-04-24 13:04:34

标签: android filepicker

使用以下方法打开文件选择器时:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent, "Open file");
startActivityForResult(chooserIntent, REQUEST_CODE_FILE_PICKER);

除非设置了默认值,否则将向用户提供要使用的文件选择器选项。如何将您自己的内部文件选择器作为呈现给用户的文件选择器之一(例如Material File Picker)提供?

1 个答案:

答案 0 :(得分:0)

感谢@ CommonsWare的评论,这里是将内部文件选择器添加到提供给用户的选项列表中所需的额外代码:

// this bit is as before...
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
Intent chooserIntent = Intent.createChooser(intent, "Open file");

// new bit... create Intent for the internal file picker...
Intent materialFilePickerIntent = new Intent(this, FilePickerActivity.class);
materialFilePickerIntent.putExtra(FilePickerActivity.ARG_FILE_FILTER, Pattern.compile(".*\\.txt$"));

// and add the picker to the list...
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { materialFilePickerIntent });

// finally, startActivityForResult() as before...
startActivityForResult(chooserIntent, REQUEST_CODE_FILE_PICKER);