如何使pdf,xlsx和txt文件android的intent.setType?
我只想从存储中选择pdf,xlsx和txt文件,但是intent.setType只能执行一个文件(例如仅仅是.txt文件(或)pdf文件)。 是否有可能通过编码intent.setType()获取所有三个文件?有没有办法做到这一点?
这是我的一些代码。
  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)扩展名。 要支持或浏览这些扩展,您需要添加更多mymeTypes。
使用下面的方法浏览REQUEST_CODE_DOC为onActivityResult的requestCode的文档(final int requestCode,final int resultCode,final Intent data)@Override方法。
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);
}
你可以得到清晰的概念,并从这里添加你需要的mymeTypes
intent.setType("image/*|application/pdf|audio/*");
也许这是你想要的。
看起来你只是想看看这些意图是否可以解决。 这可能是一个更好的方法:
 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
    }
}
参考文献:
http://developer.android.com/training/basics/intents/sending.html
我如何确定Android是否可以处理PDF
链接地址: http://www.djcxy.com/p/45441.html上一篇: how to make intent.setType for pdf,xlsx and txt file android?
