如何使pdf,xlsx和txt文件android的intent.setType?

如何使pdf,xlsx和txt文件android的intent.setType?

问题描述:

我只想从存储中选择pdf,xlsx和txt文件,但是intent.setType只能做一个文件(例如,仅txt文件(或仅pdf文件)).是否可以通过编码intent.setType()来获取所有三个文件,并且有办法吗?

I want to choose only pdf, xlsx and txt file from storage but intent.setType can do only one file(eg.txt file only (or) pdf file only). Is it possible to get all three files by coding intent.setType() and Is there a way to do?

这是我的一些代码.

  private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("application/pdf");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select txt file"),
                0);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog

    }
}

@Fatehali Asamadi的方法还可以,但是需要添加一些适当的用法. 对于Microsoft文档,都使用(.doc或.docx),(.ppt或.pptx),(.xls或.xlsx)扩展名.要支持或浏览这些扩展,您需要添加更多的mimeType.

@Fatehali Asamadi's way is OK, but need to add something for appropriate use. For Microsoft documents both (.doc or .docx), (.ppt or .pptx), (.xls or .xlsx) extensions are used. To support or browse these extensions you need to add more mimeTypes.

使用以下方法浏览文档,其中REQUEST_CODE_DOC是onActivityResult的requestCode(最终int requestCode,最终int resultCode,最终Intent数据)@Override方法.

Use below method to browse documents where REQUEST_CODE_DOC is requestCode for onActivityResult(final int requestCode, final int resultCode,final Intent data) @Override method.

private void browseDocuments(){

    String[] mimeTypes =
            {"application/msword","application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx
                    "application/vnd.ms-powerpoint","application/vnd.openxmlformats-officedocument.presentationml.presentation", // .ppt & .pptx
                    "application/vnd.ms-excel","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // .xls & .xlsx
                    "text/plain",
                    "application/pdf",
                    "application/zip"};

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
        if (mimeTypes.length > 0) {
            intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
        }
    } else {
        String mimeTypesStr = "";
        for (String mimeType : mimeTypes) {
            mimeTypesStr += mimeType + "|";
        }
        intent.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
    }
    startActivityForResult(Intent.createChooser(intent,"ChooseFile"), REQUEST_CODE_DOC);

}

您可以从[a href ="https://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc]中获得清晰的概念并添加所需的mimeType./4212908#4212908>此处

You can get clear concept and add your required mimeTypes from Here