在 Android 中的 Whatsapp 中共享多个图像和文本

问题描述 投票:0回答:1

``我想在 android 中共享带有单个标题的多个图像。我正在使用下面的代码,但它与文本一起共享图像。每个图像条目中的文本都会重复。我只想要一个图像列表,后跟单个文本标题。请检查以下代码。

有趣的分享笔记( 标题:字符串, 描述:字符串, multiplePhotoPickerUri:列表, 导航控制器:导航主机控制器, 现有图像:可变列表 ){ val imageUriArray = ArrayList()

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()
}

}`

android android-intent whatsapp compose
1个回答
0
投票

“共享”的确切行为尚未明确/记录。

接收应用程序如何处理共享数据取决于接收应用程序。在您的情况下,接收应用程序将文本元素作为标题应用到每个图像。但这完全取决于接收应用程序如何处理。

Stackoverflow 上的一些帖子表明不支持同时提供 EXTRA_TEXT 和 EXTRA_STREAM。看来这很大程度上取决于接收应用程序的实现。

请参阅使用 shareIntent 时如何通过 Intent.ACTION_SEND_MULTIPLE 发送多种数据类型?

© www.soinside.com 2019 - 2024. All rights reserved.