I'd like to use the following code for an application:
val browserIntent = Intent(Intent.ACTION_VIEW)
browserIntent.setDataAndType(Uri.parse(foo), MIME_PDF)
val chooser = Intent.createChooser(browserIntent, getString(R.string.view_using))
chooser.flags = Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(chooser)
where
const val MIME_PDF = "application/pdf"
and the format would be something like:
https://example.net/name-of-feature/ThisIsThePdf.pdf
On my own device and on an emulator, this will open a nice view for viewing the pdf, seems like it loads a google pdf viewer and it doesn't even open a launcher for picking an application, which is completely fine, however, could it be possible that for certain devices, this request would fail ?
As far as I can tell, this intent is actually just attempting to open anything which could view a pdf and then gives it the data for the pdf, so this intent has no actual dependency on the google pdf viewer, instead it just relies on the user having a browser, or anything else which could process the pdf installed, right ?
In theory then, as long as a user has (at least) a browser installed, this intent will always work ?
CodePudding user response:
Never trust that the user will have an application which will be able to open the file you wanted to. You never know what applications user has installed on his/her device and etc. Always check :
try {
startActivity(chooser)
} catch (e: ActivityNotFoundException) {
// Define what your app should do if no activity can handle the intent.
}
Better to be prepared instead of crashing the app.