A-run
A-run

Reputation: 490

ActivityNotFoundException when trying to send multiple attachments via emails

I am receiving this error when trying to send multiple attachment via email:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND_MULTIPLE (has extras) }

I have Gmail and Outlook installed in my Phone.(Samsung device with Android 11). When sending a single attachment I am not facing any issue. I am using below intent to share multiple attachment over email

fun getIntent(
    emailId: Array<String>,
    subject: String,
    body: String,
    attachmentUri: ArrayList<Uri?>
): Intent? {
        val text = ArrayList<String>()
        text.add(body)
      
        return Intent(Intent.ACTION_SEND_MULTIPLE).apply {
            putExtra(Intent.EXTRA_EMAIL, emailId)
            putExtra(Intent.EXTRA_SUBJECT, subject)
            putExtra(
                Intent.EXTRA_TEXT,
                text
            )
            putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentUri)
        }}

Above method is called in this way

        val history: ArrayList<Uri?> = getHistory()
        val intent = Utils.getIntent(
            arrayOf(getString(R.string.email_id)),
            subject,
            getString(R.string.body),
            history
        )
        intent?.let {
            try {
                startActivity(it)
            } catch (e: Exception) {
                
            }
        }

String resource:

    <string name="body">\u2022 Information:\n\u0020\u0020\u25E6Name: %s\n\u0020\u0020\u25E6Age: %s\n\u0020\u0020\u25E6%s\n</string>

What am I doing wrong?

Upvotes: 1

Views: 161

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007544

According to the documentation on ACTION_SEND_MULTIPLE, you need to specify the MIME type of your content on your Intent, and you are not doing so.

Input: getType() is the MIME type of the data being sent. get*ArrayListExtra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, you can also optionally supply EXTRA_HTML_TEXT for clients to retrieve your text with HTML formatting.

Multiple types are supported, and receivers should handle mixed types whenever possible. The right way for the receiver to check them is to use the content resolver on each URI. The intent sender should try to put the most concrete mime type in the intent type, but it can fall back to /* or / as needed.

e.g. if you are sending image/jpg and image/jpg, the intent's type can be image/jpg, but if you are sending image/jpg and image/png, then the intent's type should be image/*.

However, beyond that, it is entirely possible that a user will not have an app that supports ACTION_SEND_MULTIPLE for your requested MIME type. Make sure that you do something useful in your catch block for the try around your startActivity() call.

Upvotes: 1

Related Questions