13

I'm using Intent to open file manager, I need to know how to show only .doc, .docx files to choose by users. How to put setType to intent?` The Following function is used to choose file from file manager.

    private void showFileChooser() {
    Intent intent = new Intent();
    //sets the select file to all types of files
    intent.setType("application/*");

    //allows to select data and return it
    intent.setAction(Intent.ACTION_GET_CONTENT);
    //starts new activity to select file and return data
    startActivityForResult(Intent.createChooser(intent, "Choose File to Upload.."), PICK_FILE_REQUEST);
}`
Manoj Reddy
  • 454
  • 5
  • 13

1 Answers1

21

You can add multiple mime types like this:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, REQUEST_CODE_OPEN);

Following mime types corespond to .docx and .doc files

String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};
Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52
  • 1
    This set of commands does not work for excel files. A message shows up: **No apps can perform this action**. I used this line of command for excel files: `String[] mimetypes = {"application/vnd.ms-excel" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"};` – Aliton Oliveira Feb 06 '19 at 21:44
  • 2
    @AlitonOliveira, that is obvious at least becaus the question was about `doc`, `docx` formats. – Vladyslav Matviienko Feb 06 '19 at 22:00