fix: Ensure files are fully downloaded before sharing #482
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The previous code would share media by either:
a. If it was an image, downloading the image using Glide in to a bitmap, then recompressing as a PNG, saving, and sharing the resulting file.
b. Otherwise, create a temporary file, enqueue a DownloadManager request to download the media in to the file, and immediately start sharing, hoping that the download had completed in time.
Both approaches have problems:
In the "image" case the image was being downloaded (or retrieved from the Glide cache), decompressed to a bitmap, then recompressed as a PNG. This uses more memory, and doesn't share the original contents of the file. E.g., if the original file was a JPEG that's lost (and the PNG might well be larger than the source image).
In the second case the DownloadManager download is not guaranteed to have completed (or even started) before the user chooses the share destination. The destination could receive a partial or even empty file.
Fix both of those cases by always fully downloading the file before sending the share intent. This guarantees the file is available to share, and in its original format. Since this uses the same OkHttpClient as the rest of the app the content is highly likely to be in the OkHttp cache, so there will no extra network traffic because of this.