-
public final class StreamFileManagerManages file operations for the Stream Chat SDK, providing utilities for caching, retrieving, and managing files in various cache directories and external storage.
This class handles:
Image caching in a dedicated directory
General file caching in the Stream cache directory
Timestamped cache folders for isolated file storage
Cache cleanup operations
External storage management for photos and videos captured using the SDK
-
-
Constructor Summary
Constructors Constructor Description StreamFileManager()
-
Method Summary
Modifier and Type Method Description final FilegetImageCache(Context context)Returns the directory used for caching images. final FilegetVideoCache(Context context)Returns the directory used for caching video bytes streamed through ExoPlayer. final Result<File>createFileInCache(Context context, String fileName)Creates a file reference in cache without writing content. final Result<File>writeFileInCache(Context context, String fileName, InputStream source)Writes data from an InputStream to a cache file. final Result<File>writeFileInTimestampedCache(Context context, String fileName, InputStream source)Writes data from an InputStream to a file in a timestamped cache folder. final Result<File>getFileFromCache(Context context, String fileName)Retrieves a file from cache by name. final UnitevictCacheFiles(Context context, String prefix, Long ttlMs, Long maxSizeBytes)Evicts cached files matching prefix based on a time-to-live and a total size cap. final Result<Unit>clearCache(Context context)Clears the Stream cache directory. final Result<Unit>clearAllCache(Context context)Clears all cached data including Stream cache, image cache, and timestamped cache folders. final Result<File>createPhotoInExternalDir(Context context)Creates a file for photo capture in the external pictures directory. final Result<File>createVideoInExternalDir(Context context)Creates a file for video capture in the external movies directory. final Result<Unit>clearExternalStorage(Context context)Clears Stream files from the external storage directories. -
-
Method Detail
-
getImageCache
final File getImageCache(Context context)
Returns the directory used for caching images.
Path:
{cacheDir}/stream_image_cache/- Parameters:
context- Android context for accessing the cache directory.
-
getVideoCache
final File getVideoCache(Context context)
Returns the directory used for caching video bytes streamed through ExoPlayer.
Path:
{cacheDir}/stream_video_cache/- Parameters:
context- Android context for accessing the cache directory.
-
createFileInCache
final Result<File> createFileInCache(Context context, String fileName)
Creates a file reference in cache without writing content.
This is useful for operations like MediaRecorder which need a file path before actually writing any content.
- Parameters:
context- Android context for cache directory accessfileName- Name of the file to create
-
writeFileInCache
final Result<File> writeFileInCache(Context context, String fileName, InputStream source)
Writes data from an InputStream to a cache file.
The file is created in the Stream cache directory. If a file with the same name already exists, it will be overwritten.
- Parameters:
context- Android context for cache directory accessfileName- Name of the file to create in cachesource- InputStream containing the data to cache
-
writeFileInTimestampedCache
final Result<File> writeFileInTimestampedCache(Context context, String fileName, InputStream source)
Writes data from an InputStream to a file in a timestamped cache folder.
This method creates a unique timestamped folder (format:
STREAM_HHmmssSSS) and writes the file there. This is useful for caching files from URIs where unique folder isolation is needed to prevent naming conflicts.- Parameters:
context- Android context for cache directory accessfileName- Name of the file to create in the timestamped foldersource- InputStream containing the data to cache
-
getFileFromCache
final Result<File> getFileFromCache(Context context, String fileName)
Retrieves a file from cache by name.
- Parameters:
context- Android context for cache directory accessfileName- Name of the file to retrieve
-
evictCacheFiles
final Unit evictCacheFiles(Context context, String prefix, Long ttlMs, Long maxSizeBytes)
Evicts cached files matching prefix based on a time-to-live and a total size cap.
Deletes every file whose
lastModifiedis older than ttlMs milliseconds.If the remaining files exceed maxSizeBytes in total, deletes the oldest files first until the total drops below the cap.
- Parameters:
context- Android context for cache directory accessprefix- Filename prefix that identifies files subject to evictionttlMs- Maximum age in milliseconds; older files are always deletedmaxSizeBytes- Soft size cap in bytes; exceeded only temporarily until the next eviction pass
-
clearCache
final Result<Unit> clearCache(Context context)
Clears the Stream cache directory.
- Parameters:
context- Android context for cache directory access
-
clearAllCache
final Result<Unit> clearAllCache(Context context)
Clears all cached data including Stream cache, image cache, and timestamped cache folders.
The video cache directory is owned by a live
SimpleCachewhen the video cache is opted in, so it is cleared in-place by the owningVideoMediaCacheat a higher layer (seeChatClient.clearCacheAndTemporaryFiles) rather than by deleting the directory here.- Parameters:
context- Android context for cache directory access
-
createPhotoInExternalDir
final Result<File> createPhotoInExternalDir(Context context)
Creates a file for photo capture in the external pictures directory.
The file is created in the app-specific external pictures directory:
Primary:
{externalFilesDir}/Pictures/STREAM_IMG_{timestamp}.jpgFallback:
{cacheDir}/stream_cache/STREAM_IMG_{timestamp}.jpg(if external storage unavailable)
The file name includes a timestamp to ensure uniqueness.
- Parameters:
context- Android context for accessing storage directories
-
createVideoInExternalDir
final Result<File> createVideoInExternalDir(Context context)
Creates a file for video capture in the external movies directory.
The file is created in the app-specific external movies directory:
Primary:
{externalFilesDir}/Movies/STREAM_VID_{timestamp}.mp4Fallback:
{cacheDir}/stream_cache/STREAM_VID_{timestamp}.mp4(if external storage unavailable)
The file name includes a timestamp to ensure uniqueness.
- Parameters:
context- Android context for accessing storage directories
-
clearExternalStorage
final Result<Unit> clearExternalStorage(Context context)
Clears Stream files from the external storage directories.
This method removes all Stream-generated photos (STREAM_IMG_) from the Pictures directory and Stream-generated videos (STREAM_VID_) from the Movies directory.
Only files matching the Stream naming pattern are deleted:
Photos:
STREAM_IMG_{timestamp}.jpgVideos:
STREAM_VID_{timestamp}.mp4
- Parameters:
context- Android context for accessing external storage directories
-
-
-
-