Fix camera capture crash on devices with lower RAM - #4635
Open
xkello wants to merge 1 commit into
Open
Conversation
Coverage Report for CI Build 30919175200Coverage increased (+0.01%) to 59.147%Details
Uncovered ChangesNo uncovered changes found. Coverage Regressions2 previously-covered lines in 2 files lost coverage.
Coverage Stats
💛 - Coveralls |
📦 Build Artifacts Ready
|
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.
Description
On some devices with heavy OEM camera stacks (confirmed on Motorola, 4GB RAM), tapping "add photo" -> camera reliably crashed the app. Root cause, confirmed via multiple
adb logcatcaptures: launching the OS camera app spikes system-wide memory pressure, and Android'slowmemorykillerkills our own backgrounded process moments later - before the user can even take the photo. It's a silent OS process kill, not a Java exception (noFATAL EXCEPTIONin any capture).Fixes: #2784
What changed
CameraActivity.javagrantUriPermission()/revokeUriPermission()for the captureFileProviderURI - some OEM camera apps (confirmed: Motorola) don't reliably honorFLAG_GRANT_*flags when the URI is passed viaEXTRA_OUTPUTrather thansetData().onSaveInstanceState()/resume branch inonCreate()- if the process is killed and recreated by Android to redeliver a pending camera result, resume from the savedtargetPath/cameraFileinstead of relaunching the capture or losing track of the file.cameraFileinonActivityResult()- fails cleanly instead of aNullPointerExceptionif state wasn't restored.CameraForegroundService.java(new)oom_score_adj) for the duration the OS camera is in front, making the kill significantly less likely in the first place.AndroidManifest.xmlCameraForegroundService(foregroundServiceType="shortService",stopWithTask="true").Behaviour
Before: on affected devices, opening the camera from a form would kill the app outright - the project reloads from scratch and the in-progress edit is lost.
After: the process is kept in a protected priority tier for as long as the camera is open, verified directly via
adb-oom_score_adjstays at0/50during the camera wait, versus the700tier every observed kill happened at. If the process is still killed under extreme memory pressure,CameraActivitynow resumes and completes the capture correctly instead of crashing or silently corrupting the saved file.Before fix: #2784 (comment)
After fix:
screen-20260804-164203.mp4
TLDR @Withalion
Camera crash on some devices = OS killing our process under memory pressure while the OEM camera app is open, not a code bug. Fix is two-layered:
CameraForegroundServicereduces the odds of the kill (confirmed viaoom_score_adjmeasurement, not just crash/no-crash testing), andCameraActivity's save/resume logic makes it non-destructive if the kill happens anyway. No new user-facing UI - the foreground service's notification is intentionally not requested/shown (POST_NOTIFICATIONSnot requested), since the user doesn't need to know this is happening.