getCachedFileFromUri

fun getCachedFileFromUri(context: Context, attachmentMetaData: AttachmentMetaData): File?

Retrieves or creates a cached copy of a file from the given attachment metadata.

This method handles two scenarios:

  1. If AttachmentMetaData.file is already set, it returns the file directly

  2. 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 null instead of throwing.

Return

A File object pointing to the cached file, or null if both file and URI are null or if the content could not be read from the URI.

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.