how to make intent.setType for pdf,xlsx and txt file android?

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?

Here is some of my code.

  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'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 mymeTypes.

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

}

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


intent.setType("image/*|application/pdf|audio/*");

也许这是你想要的。


It looks like you just want to see if those intents can be resolved. This might be a better approach:

 private void showFileChooser() {

    Intent intentPDF = new Intent(Intent.ACTION_GET_CONTENT);
    intentPDF.setType("application/pdf");
    intentPDF.addCategory(Intent.CATEGORY_OPENABLE);

    Intent intentTxt = new Intent(Intent.ACTION_GET_CONTENT);
    intentTxt.setType("text/plain");
    intentTxt.addCategory(Intent.CATEGORY_OPENABLE);

    Intent intentXls = new Intent(Intent.ACTION_GET_CONTENT);
    intentXls.setType("application/x-excel");
    intentXls.addCategory(Intent.CATEGORY_OPENABLE);

    PackageManager packageManager = getPackageManager();

    List activitiesPDF = packageManager.queryIntentActivities(intentPDF,
    PackageManager.MATCH_DEFAULT_ONLY);
    boolean isIntentSafePDF = activitiesPDF.size() > 0;

    List activitiesTxt = packageManager.queryIntentActivities(intentTxt,
    PackageManager.MATCH_DEFAULT_ONLY);
    boolean isIntentSafeTxt = activitiesTxt.size() > 0;

    List activitiesXls = packageManager.queryIntentActivities(intentXls,
    PackageManager.MATCH_DEFAULT_ONLY);
    boolean isIntentSafeXls = activitiesXls.size() > 0;

    if (!isIntentSafePDF || !isIntentSafeTxt || !isIntentSafeXls){

        // Potentially direct the user to the Market with a Dialog

    }

}

References:

http://developer.android.com/training/basics/intents/sending.html

How do I determine if Android can handle PDF

链接地址: http://www.djcxy.com/p/45442.html

上一篇: 在iPhone中使用Webview查看.doc,.docx,.rtf,.ppt文件

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