Home > Software design >  How to open a file in another app via an Intent using an URI?
How to open a file in another app via an Intent using an URI?

Time:02-15

In my app, I have several file Uri (NOT URI) store. These are of the form:

content://com.android.providers.downloads.documents/document/427

My intention is, once I click a button, open one of these Uri in a file reader (may it be MS Word, Excel, PDF Reader... depending on the extension and device).

I am currently trying this snippet:

                val file = File(Uri.parse(uri).path!!)
                val myMime: MimeTypeMap = MimeTypeMap.getSingleton()
                val newIntent = Intent(Intent.ACTION_VIEW)

                val mimeType: String = myMime.getMimeTypeFromExtension(file.extension).toString()
                newIntent.setDataAndType(
                    FileProvider.getUriForFile(applicationContext, "${applicationContext.packageName}.provider", file), mimeType)
                newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                newIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
                try {
                    startActivity(newIntent)
                } catch (e: ActivityNotFoundException) {
                }

But every time I try to open it, I get:

java.lang.IllegalArgumentException: Failed to find configured root that contains /document/427

Could you please tell me what am I missing? No answers I've found here targeted my problem. Thanks in advance!

UPDATE Thanks to @CommonsWare help, I managed to avoid an Exception, my code now looks like this:

val newIntent = Intent(Intent.ACTION_VIEW)
newIntent.addCategory(Intent.CATEGORY_OPENABLE)
newIntent.data = Uri.parse(uri)
newIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
newIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
try {
    startActivity(newIntent)
} catch (e: ActivityNotFoundException) {
    Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show()
}

I'm getting the Uri from here:

val fileResultLauncher =      registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    viewModel.uploadFile(result, false)
            }

fileFab.setOnClickListener {
    val intent = Intent()
    intent.type = "application/*"
    intent.action = Intent.ACTION_OPEN_DOCUMENT
    fileResultLauncher.launch(intent)
}

Unfortunately, I'm getting a NoActivityFoundException

CodePudding user response:

Get rid of:

val file = File(Uri.parse(uri).path!!)

...and replace your setDataAndType() call with:

newIntent.setDataAndType(Uri.parse(uri), mimeType)
  • Related