From 0fa853ed1c5c879482e4c17735f2183ccb41188a Mon Sep 17 00:00:00 2001 From: Ankit Date: Wed, 14 Oct 2026 23:19:27 +0530 Subject: [PATCH] fix: catch SecurityException when persisting gallery URI permission 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 --- CHANGELOG.md | 6 ++++++ .../ioncameralib/view/IONCAMROpenPhotoPickerActivity.kt | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c016842..44e628e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 The changes documented here do not include those from the original repository. +## [Unreleased] + +### 2026-10-14 + +- Fix: catch the `SecurityException` thrown by `takePersistableUriPermission` in `IONCAMROpenPhotoPickerActivity` when the picker returns a URI without a persistable grant (seen on Android 11/12), so choosing from the gallery no longer crashes the app ([capacitor-camera#58](https://github.com/ionic-team/capacitor-camera/issues/58)). + ## [1.0.2] ### 2026-07-08 diff --git a/src/main/kotlin/io/ionic/libs/ioncameralib/view/IONCAMROpenPhotoPickerActivity.kt b/src/main/kotlin/io/ionic/libs/ioncameralib/view/IONCAMROpenPhotoPickerActivity.kt index bf77e2a..09b1333 100644 --- a/src/main/kotlin/io/ionic/libs/ioncameralib/view/IONCAMROpenPhotoPickerActivity.kt +++ b/src/main/kotlin/io/ionic/libs/ioncameralib/view/IONCAMROpenPhotoPickerActivity.kt @@ -114,7 +114,13 @@ class IONCAMROpenPhotoPickerActivity : ComponentActivity() { private fun makeUriPermissionPersistable(uri: Uri) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // only for Android >= 11 - applicationContext.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + try { + applicationContext.contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + } catch (e: SecurityException) { + // Some pickers (GMS-backported photo picker, OEM galleries on Android 11/12) hand back + // URIs with no persistable grant; the temporary read grant is enough to process the media. + Log.w("PhotoPicker", "Could not persist URI permission for $uri", e) + } } }