如何为pdf,xlsx和txt文件android制作intent.setType?

时间:2015-03-11 04:27:19

标签: android android-intent

我想从存储中仅选择pdf,xlsx和txt文件,但intent.setType只能执行一个文件(仅限eg.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

    }
}

7 个答案:

答案 0 :(得分:33)

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

使用以下方法浏览文档,其中REQUEST_CODE_DOC是onActivityResult的requestCode(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);

}

您可以获得清晰的概念,并从Here

添加所需的mimeTypes

答案 1 :(得分:16)

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

也许这就是你想要的。

答案 2 :(得分:7)

看起来你只想知道这些意图是否可以解决。 这可能是一种更好的方法:

 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

How do I determine if Android can handle PDF

答案 3 :(得分:2)

您可以使用Intent.ACTION_OPEN_DOCUMENT,

每个文档都表示为内容://由DocumentsProvider支持的URI,可以作为包含openFileDescriptor(Uri, String)的流打开,或查询DocumentsContract.Document元数据。

所有选定的文档都将返回给调用应用程序,并具有可持久的读写权限授予。如果要在设备重新启动后维护对文档的访问权限,则需要使用takePersistableUriPermission(Uri, int)显式获取可持久权限。

来电者必须通过setType(String)指明可接受的文件MIME类型。例如,要选择照片,请使用image/*。如果可以接受多个不相交的MIME类型,请在EXTRA_MIME_TYPESsetType(String)*/*中定义它们。

有关详细信息,请参阅此链接

http://developer.android.com/reference/android/content/Intent.html#ACTION_OPEN_DOCUMENT

请注意,这仅适用于API级别19 +。

答案 4 :(得分:1)

您应该使用系统函数来查找您尝试访问的文件的特定mimeType(如" application / pdf")。如果您正在处理任何未知类型,可能很难找到类型。希望这段代码可以帮到你!

String url=Environment.getExternalStorageDirectory().getAbsolutePath()+"/Saves/contacts.vcf";
File file = new File(url);
Intent intent = new Intent(Intent.ACTION_VIEW);
String mimeType=MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
intent.setDataAndType(Uri.fromFile(file), mimeType);
Intent intent1 = Intent.createChooser(intent, "Open With");
startActivity(intent1);

来源HERE

答案 5 :(得分:1)

您应该查看此链接http://www.androidsnippets.com/open-any-type-of-file-with-default-intent.html。另请阅读Mime Type。

这是我为任何文件或选定的mime类型文件实现的方式

String[] mimeTypes =
{"image/*","application/pdf","application/msword","application/vnd.ms-powerpoint","application/vnd.ms-excel","text/plain"};

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"), 0);

答案 6 :(得分:1)

简单的解决方法是

   Intent gallery=new Intent();
        gallery.setType("application/*");
        gallery.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(gallery,pick);

注意:pick是int变量。