fix: Prevent crash (maybe) when cropping images#2172
Merged
nikclayton merged 1 commit intopachli:mainfrom Mar 17, 2026
Merged
Conversation
The image cropper is sometimes crashing in production with this error:
Exception java.lang.SecurityException: Only content:// URIs are allowed for security reasons. Received: file://
at com.canhub.cropper.BitmapUtils.validateOutputUri$cropper_release (BitmapUtils.kt:437)
at com.canhub.cropper.BitmapUtils.writeBitmapToUri (BitmapUtils.kt:476)
at com.canhub.cropper.BitmapCroppingWorkerJob$start$1$1.invokeSuspend (BitmapCroppingWorkerJob.kt:96)
Not sure what's generating the file:// URL scheme, but the calling code looks
like this:
```kotlin
val tempFile = createNewImageFile(this, if (isPng) ".png" else ".jpg")
val uriNew = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", tempFile)
viewModel.cropImageItemOld = item
cropImage.launch(
CropImageContractOptions(
uri = item.uri,
cropImageOptions = CropImageOptions(
customOutputUri = uriNew,
outputCompressFormat = if (isPng) Bitmap.CompressFormat.PNG else Bitmap.CompressFormat.JPEG,
),
),
)
```
`createNewImageFile(...)` creates the new file in a directory obtained
with `context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)`, and
according to https://developer.android.com/reference/androidx/core/content/FileProvider
`getExternalFilesDir` is equivalent to `<external-files-path ... />` in
this XML file.
The existing `<external-path ... />` is equivalent to
`getExternalStorageDirectory`, which isn't used anywhere in Pachli.
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.
The image cropper is sometimes crashing in production with this error:
Not sure what's generating the file:// URL scheme, but the calling code looks like this:
createNewImageFile(...)creates the new file in a directory obtained withcontext.getExternalFilesDir(Environment.DIRECTORY_PICTURES), and according to https://developer.android.com/reference/androidx/core/content/FileProvidergetExternalFilesDiris equivalent to<external-files-path ... />in this XML file.The existing
<external-path ... />is equivalent togetExternalStorageDirectory, which isn't used anywhere in Pachli.