Share Multiple images and Text in Whatsapp in Android

41 views Asked by At

I want to share multiple images with single caption in android. I am using below code but it shares image along with text. The text got duplicated in every image entries. I just want a list of images followed by single caption of text. Please check the below codes.

fun shareNotes(
    title: String,
    description: String,
    multiplePhotoPickerUri: List<Uri?>,
    navController: NavHostController,
    existingImages: MutableList<String>
) {
    val imageUriArray = ArrayList<Uri>()

    val shareIntent = Intent()

    shareIntent.action = Intent.ACTION_SEND_MULTIPLE

    shareIntent.setPackage("com.whatsapp")

    // Set the text content (title and description)

    val textContent = "$title\n$description"

    shareIntent.putExtra(Intent.EXTRA_TEXT, textContent)

    if (multiplePhotoPickerUri.isNotEmpty()) {

        multiplePhotoPickerUri.forEach { uri ->

            if(uri != null) {

                if(existingImages.contains(uri.path)) {

                    ImageUtils.createContentUriFromAppSpecificDirectory(
                        navController.context,
                        uri.path!!
                    )?.let {
                        imageUriArray.add(it)
                    }
                } else {
                    imageUriArray.add(uri)
                }
            }
        }
        shareIntent.putParcelableArrayListExtra(
            Intent.EXTRA_STREAM,
            imageUriArray
        )
        shareIntent.setType("image/jpeg")
    } else {
        // If no images are present, just set the type to text/plain
        shareIntent.type = "text/plain"
    }
    shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
    // Verify if WhatsApp is installed
    try {
        navController.context.startActivity(shareIntent)
    } catch (ex: ActivityNotFoundException) {
        Toast.makeText(navController.context, "Kindly install whatsapp first", Toast.LENGTH_SHORT).show()
    }
}
1

There are 1 answers

3
David Wasser On BEST ANSWER

The exact behaviour of "share" is not well specified/documented.

What the receiving app does with the shared data is up to the receiving app. In your case, the receiving app is applying the text element as a caption to each image. But this is completely up to the receiving app how it handles this.

Several posts on Stackoverflow indicate that providing both EXTRA_TEXT and EXTRA_STREAM is not supported. It seems that this very much depends on the implementation of the receiving app.

See How can I sent multiple data types with Intent.ACTION_SEND_MULTIPLE when using a shareIntent?