【问题标题】:How to check if Android can open a file by known extension如何检查Android是否可以通过已知扩展名打开文件
【发布时间】:2014-04-04 06:22:45
【问题描述】:

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

【问题讨论】:

标签: android android-intent android-activity


【解决方案1】:

您可以使用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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 2014-12-22
    • 1970-01-01
    • 2014-10-30
    • 2018-02-12
    • 1970-01-01
    相关资源
    最近更新 更多