I need to create intent that will open file manager (if any installed) and I want file manager to show only txt files to user, so user couldn't choose file with wrong extension.
private static final int FILE_SELECT_CODE = 0;
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select txt file"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this.getActivity(), "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == Activity.RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String path = uri.getPath();
input = new File(path);
try {
txtParser = new TxtFileParser(input);
} catch (FileNotFoundException e) {
/* Exception handler must be here! */
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
I managed to do this. But I don't know how to define txt extensions.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…