Attachment Downloads

The Stream Chat SDK supports uploading photos, audio, voice recordings, videos, and other file types. Uploaded files are represented as message attachments. The SDK also provides a convenient way to download these attachments to the device's public Downloads directory using Android's DownloadManager.

Downloading Attachments

Use the ChatClient.downloadAttachment() extension function to download an attachment:

public fun ChatClient.downloadAttachment(
    context: Context,
    attachment: Attachment,
): Call<Unit>

Parameters

ParameterDescription
contextThe Android context.
attachmentThe Attachment object to download.

Basic Usage

val attachment: Attachment = message.attachments.first()

ChatClient.instance().downloadAttachment(
    context = context,
    attachment = attachment,
).enqueue { result ->
    when (result) {
        is Result.Success -> {
            // Download started successfully
        }
        is Result.Failure -> {
            // Handle error
        }
    }
}

Default Behavior

The download:

  • Saves to the public Downloads directory (Environment.DIRECTORY_DOWNLOADS)
  • Uses the attachment's name, title, or a fallback name derived from the URL
  • Shows a notification when the download completes (VISIBILITY_VISIBLE_NOTIFY_COMPLETED)