android – Intent.createChooser for opening pdf and epub files-ThrowExceptions

Exception or error:

My app generates PDFs or EPUBs, and when the file creation is done, I need to prompt the user for opening the file in another app.

At the moment, my app is able to create the file, and the prompt for sharing the file is showing.

The problem is that this prompt is asking to choose an app for sending the file, while I need to open the file with an app like google play books, or any epub reader. If the file is a pdf, I guess I would want to see onedrive, or my printer app.

This is the code I have :

    private fun openFile(fileName: String) {

        val file = File(absolutePath, fileName)
        val uri = Uri.fromFile(file).normalizeScheme()
        val mimeValue = getMimeType(uri.toString())

        val contentUri = FileProvider.getUriForFile(
        if (uri != null) {
            val intent = ShareCompat.IntentBuilder
   = contentUri
            requireContext().startActivity(Intent.createChooser(intent, "Open file with"))

Thx !

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *