Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Check if any app exists that can open an xps file

I have an app that needs to open an XPS file. I can use the code below to fire an Intent to open the file, however I want to know if there is a way to check if there are any apps on the phone that can handle the request to open this file.

The idea is that if no app on the phone can open an XPS file, I can prompt the user to download an app to open the XPS file.

    Intent intent = new Intent();
    intent.setAction(android.content.Intent.ACTION_VIEW);
    File file = new File(fileURL.toURI());
    intent.setDataAndType(Uri.fromFile(file), "application/vnd.ms-xpsdocument");
    //intent.setDataAndType(Uri.fromFile(file), "application/*");
    startActivity(intent); 

Any ideas?

like image 778
Ali Avatar asked Feb 03 '26 08:02

Ali


1 Answers

I've never used it but I think you can do this using PackageManager.queryIntentActivities(...)

Create the Intent you'd normally use for opening an XPS file and call that method. If the returned List<ResolveInfo> is empty then there are no activities registered for handling XPS files.

like image 82
Squonk Avatar answered Feb 05 '26 21:02

Squonk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!