从SD卡或Android中的任何应用程序中选择docx,doc,rtf,pdf类型的文件

时间:2016-04-22 10:22:14

标签: android android-intent document android-gallery android-external-storage

目前我正在尝试从SD卡或任何应用程序中选择特定类型的文件,但我失败了,我的代码如下,

Intent intent;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
            intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        else
            intent = new Intent(Intent.ACTION_GET_CONTENT);

        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("application/pdf|application/msword|application/vnd.openxmlformats-officedocument.wordprocessingml.document" +
                "|application/vnd.openxmlformats-officedocument.wordprocessingml.template" +
                "|application/vnd.ms-word.document.macroEnabled.12" +
                "|application/vnd.ms-word.template.macroEnabled.12" +
                "!application/vnd.ms-word.template.macroEnabled.12|application/rtf");

        startActivityForResult(intent, Constants.ImagePicker.REQ_PICK_RESUME);

但它只选择每种类型的文件。

1 个答案:

答案 0 :(得分:0)

试试这段代码:

Intent intent = new Intent();
intent.setType("application/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select file"),1 );