-
public final class StorageHelperHelper class for managing file and media attachments from device storage.
This class provides utilities to:
Query files and media from the device's MediaStore
Cache remote or content URI files to local storage
Parse attachment metadata from content URIs
The class uses Android's MediaStore API to retrieve file information and should be called from a background thread when querying large datasets to avoid blocking the main thread.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public classStorageHelper.Companion
-
Constructor Summary
Constructors Constructor Description StorageHelper()
-
Method Summary
Modifier and Type Method Description final FilegetCachedFileFromUri(Context context, AttachmentMetaData attachmentMetaData)Retrieves or creates a cached copy of a file from the given attachment metadata. final List<AttachmentMetaData>getFileAttachments(Context context)Retrieves all file attachments from the device's external storage. final List<AttachmentMetaData>getMediaAttachments(Context context)Retrieves all media attachments (images and videos) from the device's external storage. final List<AttachmentMetaData>getAttachmentsFromUriList(Context context, List<Uri> uriList)Queries a list of content URIs and returns parsed attachment metadata for each. -
-
Method Detail
-
getCachedFileFromUri
final File getCachedFileFromUri(Context context, AttachmentMetaData attachmentMetaData)
Retrieves or creates a cached copy of a file from the given attachment metadata.
This method handles two scenarios:
If AttachmentMetaData.file is already set, it returns the file directly
If only AttachmentMetaData.uri is available, it copies the content to a cache file
The cached file is stored in a unique timestamped folder within the app's cache directory to prevent naming conflicts. The file name is derived from the attachment's title with proper extension handling.
Content URIs backed by cloud storage providers (e.g. Google Drive) may fail to provide an input stream if the file is not locally available. In such cases this method returns
nullinstead of throwing.- Parameters:
context- The Android context used to access the content resolver and cache directory.attachmentMetaData- The attachment metadata containing either a file or URI reference.
-
getFileAttachments
final List<AttachmentMetaData> getFileAttachments(Context context)
Retrieves all file attachments from the device's external storage.
This method queries the MediaStore for all files that have a valid MIME type, excluding folders and files with unknown types. The results are sorted by date added in descending order (most recent first).
Note: This method performs a potentially expensive query operation and should be called from a background thread to avoid blocking the UI.
- Parameters:
context- The Android context used to access the content resolver.
-
getMediaAttachments
final List<AttachmentMetaData> getMediaAttachments(Context context)
Retrieves all media attachments (images and videos) from the device's external storage.
This method queries the MediaStore specifically for files with media type IMAGE or VIDEO, filtering out all other file types. The results are sorted by date added in descending order (most recent first).
The returned metadata includes video duration for video files, which is useful for displaying in the UI or for validation purposes.
Note: This method performs a potentially expensive query operation and should be called from a background thread to avoid blocking the UI.
- Parameters:
context- The Android context used to access the content resolver.
-
getAttachmentsFromUriList
final List<AttachmentMetaData> getAttachmentsFromUriList(Context context, List<Uri> uriList)
Queries a list of content URIs and returns parsed attachment metadata for each.
This method is useful when you have specific URIs (e.g., from an intent or file picker) and need to extract their metadata for attachment processing. Each URI is queried individually using the content resolver to retrieve:
Display name
MIME type (with fallback to content resolver type)
File size
URIs that cannot be queried or return no data are filtered out from the result. The attachment type (image, video, or file) is automatically determined based on the MIME type.
IMPORTANT: This method performs a potentially expensive query operation and should be called from a background thread to avoid blocking the UI.
- Parameters:
context- The Android context used to access the content resolver.uriList- The list of content URIs (using thecontent://scheme) to query.
-
-
-
-