fix: out of memory when open large file on android - #45
Merged
Conversation
Owner
|
Thanks a lot! Works for me as well |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
fix: work around file_selector_android OOM on large file picks
Problem
Picking a large file (≥ ~195 MB) on Android crashes with:
Root cause:
file_selector_android ≤ 0.5.2+6reads the entire picked file into aByteArrayOutputStreamon the Java side and encodes it as a raw byte array through Flutter'sStandardMessageCodecplatform channel. For files larger than a few hundred MB this exhausts Android's heap before any Dart code runs.An upstream fix has been proposed (flutter/flutter#141002) that would change the Java side to skip the byte read when a cache path is already available, and change the Dart side to prefer
XFile(path)overXFile.fromData(bytes). However, the proposed Dart-only snippet is insufficient on its own — the OOM occurs on the Java side before the channel message is delivered to Dart, so both halves must ship together. No fixed release is available yet.Solution
Bypass
file_selector_androidforpickFileson Android using a directMethodChannelimplementation inside the app that never puts file bytes on the platform channel.Flow:
MainActivityregisters aMethodChannel(com.example.drift/file_picker)pickFiles, opensIntent.ACTION_OPEN_DOCUMENT(system file picker, multi-select)onActivityResult, streams each content URI tocacheDir/drift_picked/in 64 KB chunks viaInputStream.copyTo()— no large heap allocationSendSelectionPicker.pickFiles()detectsPlatform.isAndroidand delegates toAndroidFilePicker; all other platforms continue usingfile_selectorunchangedFiles changed
android/app/src/main/kotlin/…/MainActivity.ktMethodChannelhandler withIntent.ACTION_OPEN_DOCUMENT, streaming copy to cache, filename resolution viaOpenableColumnslib/platform/android_file_picker.dartlib/features/send/application/send_selection_picker.dartAndroidFilePicker; non-Android path unchangedFuture cleanup
Once a fully patched
file_selector_android(both Java and Dart sides) is published, thePlatform.isAndroidbranch in send_selection_picker.dart, android_file_picker.dart, and theMainActivitychannel can all be removed and the code reverted to usingopenFiles()directly.