androidandroid-intent

How to choose only doc,docx files through intent?


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);
}`

Solution

  • 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"};