CometChat Pro Documentation

You'll find comprehensive guides and documentation to help you start working with CometChat as quickly as possible. Let's jump right in!

Thumbnail Generation

Learn how you can show a thumbnail preview of an image or video.

The Thumbnail Generation extension will help you generate a thumbnail preview of an image or a video message.

We generate metadata with a small, medium and large thumbnail for every image and video.

"@injected": {
  "extensions": {
    "thumbnail-generation": {
      "url_small": "https://data-eu.cometchat.io/...png",
      "url_medium": "https://data-eu.cometchat.io/...png",
      "url_large": "https://data-eu.cometchat.io/...png"
    }
  }
}

At the recipients' end, in the message object, you can fetch the metadata by calling the getMetadata() method. Using this metadata, you can fetch the URLs for the thumbnails.

JSONObject metadata = message.getMetadata();
if (metadata != null) {
  JSONObject injectedObject = metadata.getJSONObject("@injected");
  if (injectedObject != null && injectedObject.has("extensions")) {
    JSONObject extensionsObject = injectedObject.getJSONObject("extensions");
    if (extensionsObject != null && extensionsObject.has("thumbnail-generation"))
        {
          JSONObject thumbnailGenerationObject = extensionsObject.getJSONObject("thumbnail-generation");
          String smallUrl = thumbnailGenerationObject.getString("url_small");
          String mediumUrl = thumbnailGenerationObject.getString("url_medium");
          String largeUrl = thumbnailGenerationObject.getString("url_large");

        }
    }
}
if (metadata != null) {
  if (metadata.has("@injected")) {
     val injectedJSONObject = metadata.getJSONObject("@injected")
     if (injectedJSONObject != null && injectedJSONObject.has("extensions")) {
     val extensionsObject = injectedJSONObject.getJSONObject("extensions")

if (extensionsObject != null && extensionsObject.has("thumbnail-generation")){ val thumbnailGenerationObject = extensionsObject.getJSONObject("thumbnail-generation")

     if (thumbnailGenerationObject.has("url_small"))
  val url_small= thumbnailGenerationObject.getString("url_small")
     if (thumbnailGenerationObject.has("url_medium"))
  val url_medium= thumbnailGenerationObject.getString("url_medium")
     if (thumbnailGenerationObject.has("url_large"))
  val url_large= thumbnailGenerationObject.getString("url_large")
     }
    }
   }
  }

While thumbnails for images are usually generated within milliseconds, generating video thumbnails might take a little longer. We recommending checking if the file exists before showing the preview. You can set a simpler timer to check every second if the difference between message sentAt and the current time is less than 5 seconds.

Thumbnail Generation


Learn how you can show a thumbnail preview of an image or video.

Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.