简体   繁体   English

如何在Android中为文件创建隐式Intent

[英]How can I create implicit Intent for file in Android

I have a XML file on the external storage, and I have different apps which are able to open this file format. 我在外部存储器上有一个XML文件,并且我有不同的应用程序可以打开此文件格式。

I am going to sent implicit intent like this in order to find apps to open XML file: 我将像这样发送隐式意图,以便找到打开XML文件的应用程序:

public static boolean isIntentAvailable(Context context, Intent intent) {
    List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,
            PackageManager.MATCH_DEFAULT_ONLY);
    return !list.isEmpty();
}

This method has 2 arguments. 此方法有2个参数。 The question is how to create intent for second argument? 问题是如何为第二个论点创建intent

I have only path to file which I need to open and I tried this way: 我只有需要打开的文件路径,我尝试过这种方式:

Intent intent = new Intent(**path to file**);

But it doesn't work... How can I create this intent properly? 但这不起作用...如何正确创建此意图?

You could do something like this: 您可以执行以下操作:

File file = new File("path to file");

//checking if the File exists
if (file.exists()) {
    Intent intent = new Intent();

    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file), "text/xml");

    //checking if an Activity exists that can handle this Intent
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM