简体   繁体   English

如何以编程方式使用Intents打开文件?

[英]How to open file using Intents programatically?

I have following code for opening files programatically: 我有以下代码以编程方式打开文件:

    Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, type);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

But now I need to set MIME-type of file, and it isn't universal method for all files. 但是现在我需要设置文件的MIME类型,并且它不是所有文件的通用方法。 Is there any mean for opening file through Intent without defining type? 通过Intent打开文件而不定义类型有什么意思吗?

Sure, just use intent.setData() , instead of intent.setDataAndType() . 当然,只需使用intent.setData()而不是intent.setDataAndType() Although, the result may not be as desirable as when the type is defined. 虽然,结果可能不如定义类型时理想。

You can also use a convenience Intent constructor, and remove the "setData" call, like so: 您还可以使用便捷的Intent构造函数,并删除“ setData”调用,如下所示:

Intent intent = new Intent(Intent.ACTION_VIEW, path);

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

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