如何检查Android是否可以通过已知扩展名打开文件

时间:2014-03-03 09:20:15

标签: android android-intent android-activity

如果没有为阅读器显示下载链接,我想检查android是否有默认的Excel阅读器。这可能吗?

1 个答案:

答案 0 :(得分:2)

您可以使用PackageManager.resolveActivity()确定是否安装了可以查看该文件的内容。这是一个例子:

PackageManager pm = getPackageManager();
File file = new File("filename"); // This is the file you want to show
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(file));
// Determine if Android can resolve this implicit Intent
ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

if (info != null) {
    // There is something installed that can VIEW this file)
} else {
    // Offer to download a viewer here
}