/ SDKs / Android
SDKs
Chat SDKs Android v4
Chat SDKs Android
Chat SDKs
Android
Version 4

Generate thumbnails of a file message

Copy link

When sending image or video files, you can create thumbnails of the multimedia files and render them into your UI. You can specify up to three different sizes for thumbnail images when sending a message both with a single file and with multiple files.

Note: Supported file types are image/* and video/*. The Chat SDK doesn't support creating thumbnails when sending a file message via URL.

The sendFileMessage() method requires passing a FileMessageCreateParams object as an argument to a parameter. It contains an array of ThumbnailSize objects which specify the maximum values of width and height of each thumbnail image with the ThumbnailSize constructor. The completionHandler callback subsequently returns the array of ThumbnailSize objects that each contain the URL of the generated thumbnail image file.

A thumbnail image is generated to fit within the bounds of the provided maxWidth and maxHeight. If the size of the original image is smaller than the specified dimensions, the original image will have the width and height of the specified dimensions. The URL of the thumbnail returns the location of the generated thumbnail file within the Sendbird server.

KotlinKTX
// Create and add a thumbnailSizeList object. Up to three thumbnail sizes are allowed.
val thumbnailSizeList = listOf(
    ThumbnailSize(100, 200),
    ThumbnailSize(200, 200)
)
val params = FileMessageCreateParams().apply {
    file = FILE
    fileName = FILE_NAME
    fileSize = FILE_SIZE
    mimeType = MIME_TYPE
    thumbnailSizes = thumbnailSizeList
}

groupChannel.sendFileMessage(params) { message, e ->
    if (e != null) {
        // Handle error.
    }
    if (message == null) return@sendFileMessage
    val first = message.thumbnails[0]
    val second = message.thumbnails[1]
    val maxHeightFirst = first.maxHeight // 100
    val maxHeightSecond = second.maxHeight // 200

    val urlFirst = first.url // The URL of first thumbnail file.
    val urlSecond = second.url // the URL of second thumbnail file.
}

The same applies to sending a message with multiple files. First, create a MultipleFilesMessageCreateParams object with UploadableFileInfo which sets ThumbnailSizes, a list of three ThumbnailSize instances. They specify the maximum values of width and height of each thumbnail image. Then pass it as an argument in to sendMultipleFilesMessage(). Then the completionHandler callback subsequently returns the array of ThumbnailSize objects that each contain the URL of the generated thumbnail image file.

KotlinKTX
val thumbnailSizeList = listOf(
    ThumbnailSize(100, 200),
    ThumbnailSize(200, 200)
)
val uploadableFileInfo = UploadableFileInfo(
    file = FILE,
    fileName = FILE_NAME,
    fileSize = FILE_SIZE,
    fileType = MIME_TYPE,
    thumbnailSizes = thumbnailSizeList
)
val uploadableFileInfo2 = UploadableFileInfo(
    file = FILE_2,
    fileName = FILE_NAME_2,
    fileSize = FILE_SIZE_2,
    fileType = MIME_TYPE_2,
    thumbnailSizes = thumbnailSizeList
)
val uploadableFileInfoList = listOf(
    uploadableFileInfo,
    uploadableFileInfo2
)
val params = MultipleFilesMessageCreateParams(uploadableFileInfoList)

channel.sendMultipleFilesMessage(params) { message, e ->
    if (e != null) {
        // Handle error.
    }
    if (message == null) return@sendMultipleFilesMessage
    val first = message.files[0].thumbnails[0]
    val second = message.files[0].thumbnails[1]
    val maxHeightFirst = first.maxHeight // 100
    val maxHeightSecond = second.maxHeight // 200

    val urlFirst = first.url // The URL of first thumbnail file.
    val urlSecond = second.url // the URL of second thumbnail file.
}