public fun ChatClient.downloadAttachment(
context: Context,
attachment: Attachment,
generateDownloadUri: (Attachment) -> Uri,
interceptRequest: DownloadManager.Request.() -> Unit = {},
): Call<Unit>This is beta documentation for Stream Chat Android SDK v7. For the latest stable version, see the latest version (v6)
.
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:
Parameters
| Parameter | Description |
|---|---|
context | The Android context. |
attachment | The Attachment object to download. |
generateDownloadUri | A lambda that generates the download Uri from the attachment. Typically returns attachment.assetUrl or attachment.imageUrl parsed as a URI. |
interceptRequest | Optional lambda to customize the DownloadManager.Request before enqueueing (e.g., add headers, change notification visibility). |
Basic Usage
val attachment: Attachment = message.attachments.first()
ChatClient.instance().downloadAttachment(
context = context,
attachment = attachment,
generateDownloadUri = { it.assetUrl?.toUri() ?: it.imageUrl?.toUri()!! },
interceptRequest = { /* add custom headers, change notification visibility... */ },
).enqueue { result ->
when (result) {
is Result.Success -> {
// Download started successfully
}
is Result.Failure -> {
// Handle error
}
}
}Default Behavior
The download:
- Saves to the public
Downloadsdirectory (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)