Skip to content

fix: catch SecurityException when persisting gallery URI permission - #21

Open
ankit-edii wants to merge 1 commit into
ionic-team:mainfrom
ankit-edii:fix/gallery-persistable-uri-crash
Open

ankit-edii wants to merge 1 commit into
ionic-team:mainfrom
ankit-edii:fix/gallery-persistable-uri-crash

Conversation

@ankit-edii

Copy link
Copy Markdown

Description

On Android 11 and up, IONCAMROpenPhotoPickerActivity.makeUriPermissionPersistable calls ContentResolver.takePersistableUriPermission with no error handling. If the picker returns a URI that has no persistable grant, the call throws SecurityException inside the activity-result callback, and the whole app crashes.

This PR wraps the call in try/catch (SecurityException) and logs a warning. The picker's temporary read grant is still there, so the media is processed the same way it already is on Android 10 and below, where this method does nothing.

Context

Fixes ionic-team/capacitor-camera#58

We see this crash in production in a Capacitor app on @capacitor/camera 8.2.1 (ioncamera-android 1.0.2) when users call chooseFromGallery. Affected devices run Android 11–12 (SDK 30–32), mostly low-RAM Redmi and realme phones. On these devices PickVisualMedia falls back to a picker (the GMS-backported photo picker or an OEM gallery) that returns URIs such as content://media/external/file/40736, which have no persistable grant. The top frames match the stack trace in the issue:

java.lang.SecurityException: No persistable permission grants found for UID ... and Uri content://media/external/file/...
  at android.content.ContentResolver.takePersistableUriPermission
  at io.ionic.libs.ioncameralib.view.IONCAMROpenPhotoPickerActivity.makeUriPermissionPersistable(IONCAMROpenPhotoPickerActivity.kt:117)
  at io.ionic.libs.ioncameralib.view.IONCAMROpenPhotoPickerActivity.launchSinglePicker$lambda$1(IONCAMROpenPhotoPickerActivity.kt:34)

Type of changes

  • Fix (non-breaking change which fixes an issue)
  • Feature (non-breaking change which adds functionality)
  • Refactor (cosmetic changes)
  • Breaking change (change that would cause existing functionality to not work as expected)

Tests

  • ./gradlew testReleaseUnitTest: 60 tests, 0 failures.
  • I built the release AAR from this branch and compared it with the published io.ionic.libs:ioncamera-android:1.0.2 from Maven Central using javap -p -c. 76 of the 77 classes are identical. IONCAMROpenPhotoPickerActivity differs only by the new try/catch around takePersistableUriPermission, plus constant-pool renumbering. The manifest and resources are unchanged.
  • I haven't reproduced the crash on an affected device, so the fix is verified by the build, the unit tests and the bytecode comparison only.
  • The project has no Robolectric setup and this Activity has no tests, so I didn't add one. I'm happy to add one if you want it.

I left the version bump for your release process.

Checklist

  • Code follows code style of this project
  • CHANGELOG.md file is correctly updated
  • Changes require an update to the documentation
    • Documentation has been updated accordingly

🤖 Generated with Claude Code

makeUriPermissionPersistable called takePersistableUriPermission on
Android 11+ with no error handling. Some pickers (GMS-backported photo
picker, OEM galleries on Android 11/12) return URIs without a
persistable grant, so the call threw SecurityException inside the
activity-result callback and crashed the app.

Catch the exception and log a warning. The temporary read grant is
still there, so the media is processed as it already is on Android 10
and below, where this method does nothing.

Fixes ionic-team/capacitor-camera#58

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

[Bug]: Crash SecurityException: No persistable permission grants found in IONCAMROpenPhotoPickerActivity

1 participant