Java – Android: Check for the presence of an application that can open xps files

Android: Check for the presence of an application that can open xps files… here is a solution to the problem.

Android: Check for the presence of an application that can open xps files

I have an application that needs to open an XPS file.
I can trigger an intent to open a file using the code below, but I’m wondering if there’s a way to check if there is any app on the phone that can handle the request file to open this file.

My idea is that if there isn’t any app on the phone to open the 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?

Solution

I’ve never used it, but I think you can use PackageManager.queryIntentActivities(…) to complete this operation

To create an intent, you would typically use it to open an XPS file and call the method. If the List<ResolveInfo> is returned, no activity is registered to process XPS files.

Related Problems and Solutions