Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android): Prevent out of memory errors on Android when selecting large media (video) files from the gallery picker. #923

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Exigerr
Copy link

@Exigerr Exigerr commented Jan 21, 2025

Platforms affected

Android

Motivation and Context

Changes from #906 resulted in all gallery picker results being read in full into a byte array.
See this line.

Reading in the data fully into memory works generally fine when dealing with images.
When handling larger files (videos for example), it will nearly always result in an out of memory error (reading too much data in one go).
With videos no transformation is required, the URI simply need to be returned.

Steps to recreate issue:

  • On Android, trigger camera.getPicture with
    • destinationType as Camera.DestinationType.FILE_URI
    • mediaType as Camera.MediaType .VIDEO
    • sourceType as Camera.PictureSourceType.PHOTOLIBRARY
  • Select a large video (typically > 200mb will do it for most devices)
  • App will crash due to an Out of Memory exception
    • Similar to

    AndroidRuntime: java.lang.OutOfMemoryError: Failed to allocate a 268435472 byte allocation with 100663296 free bytes and 123MB until OOM, target footprint 239130480, growth limit 268435456

Description

Re-ordered and cleaned up the code for processResultFromGallery to only read the file data when required.
If its a video or MIME type which it cannot transform, then simply return the URI.
Similarly if the special case for just returning the path without scaling is met, avoid reading in any data.

Testing

  • Tested image selection from gallery on Pixel 6a (Android 14 & Android 15) and Samsung S10 (Android 12) devices.
    • Range of different sized (MB) images.
  • Tested video selection from gallery on Pixel 6a (Android 14 & Android 15) and Samsung S10 (Android 12) devices.
    • Range of different sized (MB) videos (10MB - 1GB).

No issues spotted. Images and videos were selected and saved into application storage without issue.

Checklist

  • I've run the tests to see all new and existing tests pass
  • I added automated test coverage as appropriate for this change
  • Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • I've updated the documentation if necessary

…large media (video) files from the gallery picker.

Changes from apache#906 resulted in all gallery picker results being read in full into a byte array. This works fine when dealing with images, however when larger files (videos for example) are selected it will nearly always result in an out of memory error (reading too much data in one go). With videos no transformation is required, the URI simply need to be returned.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant