user3198245
user3198245

Reputation: 25

How to check if Android can open a file by known extension

I would like to check if android have a default excel reader if not show a download link for a reader . Is that possible ?

Upvotes: 2

Views: 1289

Answers (1)

David Wasser
David Wasser

Reputation: 95618

You can use PackageManager.resolveActivity() to determine if there is something installed that can view the file. Here's an example:

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
}

Upvotes: 3

Related Questions