CanHub / Android-Image-Cropper

Image Cropping Library for Android, optimised for Camera / Gallery.
https://canhub.github.io/
Apache License 2.0
1.17k stars 240 forks source link

Runtime Error Occurs When Revoking Camera Permission During Image Cropping #627

Closed Jaypatelbond closed 2 months ago

Jaypatelbond commented 2 months ago

Description

Issue Overview:

Upon initiating image cropping in the Android application using the provided library, a runtime error is encountered. The error is specifically identified as a java.lang.SecurityException: Permission Denial, arising when the camera permission is revoked during the process. This error disrupts the flow of the application and negatively impacts user experience.

Error Details:

The error message is as follows:

Fatal Exception: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.sec.android.app.camera/.Camera clip={text/uri-list hasLabel(0) {U(content)}} (has extras) } from ProcessRecord{3b739d2 31998:com.test.app/u0a418} (pid=31998, uid=10418) with revoked permission android.permission.CAMERA

Steps to Reproduce:

  1. Grant camera permission to the application.
  2. Initiate image cropping using the library's provided method (cropImage.launch(options { setGuidelines(Guidelines.ON) })).
  3. While the camera intent is open, revoke the camera permission.
  4. Proceed with capturing the image and then navigate back to the application.

Expected Behavior:

After revoking the camera permission during the image cropping process, the application should handle the permission change gracefully, either by prompting the user to grant permission again or by appropriately handling the lack of permission without causing a runtime error.

Proposed Solution:

Implement a permission check within the library's codebase, specifically during the image cropping process. This check should ensure that the necessary permissions are still granted before proceeding with any camera-related operations. Additionally, consider implementing error handling mechanisms to gracefully handle scenarios where permissions are revoked during critical operations.

Additional Information:

cropImage.launch( options { setGuidelines(Guidelines.ON) } )

This is the working recording when permission is given and not revoked during the cropping. https://github.com/CanHub/Android-Image-Cropper/assets/29830677/54fc4643-59c5-411c-9903-ef3c1af9edef

This is the crash recording when encountered. Multiple people also raised this issue. https://github.com/CanHub/Android-Image-Cropper/assets/29830677/b3168db1-cd11-4217-b8e8-81ecf89b7e4c

vanniktech commented 2 months ago

Probably, I want to get rid of those contracts and all the activity launching. Ideally only the CropImageView would be provided and then it's up to you to handle everything correctly. So no plans to fix it.