I'm currently stuck with this situation and there appears to be no question asked for this specific issue.
I'm trying to open a .txt
file after downloading and I want to check if the user has an app that will be able to open that file before actually downloading it.
On my emulated phone running API 26 there is a native HTML editor that is capable of opening text files and the Chrome browser app as well.
I've already opened those apps for the first time just in case this could be an impediment and I am still unable to get this to work.
To check if the file can be opened I am using the following method:
getPackageManager().queryIntentActivities(intent, 0);
And this never works, it always returns 0 activities, but as soon as I install a dedicated text editor then that one returns in the query, but just the one. However, once I proceed with the prompt menu it lists 3 ways to open the text file: chrome browser, html editor and the text editor.
So it is recognizing the chrome browser and html editor as capable of reading .txt
files while prompting, but the query is clearly unable to do so.
This is how I build the Intent:
intent = new Intent(Intent.ACTION_VIEW);
intent.setType(fileExtension); // this can be a String .txt .pdf etc
How can I find out if there actually exists apps capable of opening a file when the queryIntentActivities
is clearly not working correctly?
I don't know if you intentionally left out the URI or not (setData()
), but without it, it won't work properly.
Additionally, without a URI, this might vary between devices too because, with your code on my devices, I do not even see Chrome and HTML editor, regardless before or after I dl a text editor. Yes, it's weird.
Anyways, sticking to your code, just add a dummy uri, and you'll be able to pull all the correct apps:
Uri uri = Uri.parse("content://dummy");
intent.setDataAndType(uri, "text/plain");
But if you do have a valid URI before checking this resolve, then I am not seeing your issue. Fake URI or not, I am able to see all the apps that can handle it.
Sidenote: with a valid URI, it's recommended to use FileProvider
now, see: