Android-Image-Cropper
Android-Image-Cropper copied to clipboard
[BUG] - Crash when camera permission is present in manifest
- Lib Version: 4.3.1
Describe the bug
When having <uses-permission android:name="android.permission.CAMERA" />
in AndroidManifest and select Camera
as the source, the app crashes due to missing camera permission.
I saw that the permission stuff got removed in 4.0.0 in favor of using ActivityResultContracts (which is awesome actually) but we need that permission for another feature in our app. As soon as you remove the permission from the manifest, selecting camera as source doesn't crash anymore.
Stacktrace
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE flg=0x3 cmp=com.android.camera2/com.android.camera.CaptureActivity clip={text/uri-list hasLabel(0) {U(content)}} (has extras) } from ProcessRecord{5163a5b 17423:de.nowdev.imagecropperbug/u0a253} (pid=17423, uid=10253) with revoked permission android.permission.CAMERA
at android.os.Parcel.createExceptionOrNull(Parcel.java:3011)
at android.os.Parcel.createException(Parcel.java:2995)
at android.os.Parcel.readException(Parcel.java:2978)
at android.os.Parcel.readException(Parcel.java:2920)
at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:2043)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1801)
at android.app.Activity.startActivityForResult(Activity.java:5470)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:705)
at androidx.core.app.ActivityCompat$Api16Impl.startActivityForResult(ActivityCompat.java:766)
at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:241)
at androidx.activity.ComponentActivity$2.onLaunch(ComponentActivity.java:237)
at androidx.activity.result.ActivityResultRegistry$2.launch(ActivityResultRegistry.java:175)
at androidx.activity.result.ActivityResultLauncher.launch(ActivityResultLauncher.java:47)
at com.canhub.cropper.CropImageActivity.openCamera(CropImageActivity.kt:133)
at com.canhub.cropper.CropImageActivity.openSource(CropImageActivity.kt:125)
at com.canhub.cropper.CropImageActivity.access$openSource(CropImageActivity.kt:24)
at com.canhub.cropper.CropImageActivity$onCreate$1.invoke(CropImageActivity.kt:68)
at com.canhub.cropper.CropImageActivity$onCreate$1.invoke(CropImageActivity.kt:68)
at com.canhub.cropper.CropImageActivity.showImageSourceDialog$lambda-10(CropImageActivity.kt:159)
at com.canhub.cropper.CropImageActivity.$r8$lambda$8d0SyBfqZBq2EMFA6nOVWjQNv2s(Unknown Source:0)
at com.canhub.cropper.CropImageActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at androidx.appcompat.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1068)
at android.widget.AdapterView.performItemClick(AdapterView.java:330)
at android.widget.AbsListView.performItemClick(AbsListView.java:1197)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3210)
at android.widget.AbsListView$3.run(AbsListView.java:4176)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.wm.ActivityTaskSupervisor.checkStartAnyActivityPermission(ActivityTaskSupervisor.java:1108)
at com.android.server.wm.ActivityStarter.executeRequest(ActivityStarter.java:1013)
at com.android.server.wm.ActivityStarter.execute(ActivityStarter.java:702)
at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1240)
at com.android.server.wm.ActivityTaskManagerService.startActivityAsUser(ActivityTaskManagerService.java:1203)
To Reproduce Steps to reproduce the behavior:
- Add
<uses-permission android:name="android.permission.CAMERA" />
to AndroidManifest - Launch Image Cropper via
CropImageContract()
:cropImage.launch(options {})
- Select Camera as source
- See crash
Expected behavior Library still uses the new way via ActivityResultContract and just ignores the presence of the camera permission
Smartphone (please complete the following information):
- simply test on Emulator or any device (API >= 23 because of permission I guess)
Additional context If you need a sample project, just ping me again. I can provide one.
Not sure how to solve, open for suggestions and/or a PR
Found out that this behaviour is actually described in the docs too: https://developer.android.com/reference/android/provider/MediaStore#ACTION_IMAGE_CAPTURE 😟
For now we use the workaround to request the camera permission on our side in advance. Not sure if we could do something in the library to at least prevent crashes in this situation.
But I think it would be worth to note that circumstance in the readme. What do you think?
@PhilippNowak96 We can add on the FAQ documentation, could you please drop the PR?
Would avoid README since is not this library specific
Let's keep this mess organised! This issue has been automatically marked as stale
because it has not had recent activity. =( It will be closed if no further activity occurs. Thank you for your contributions.
Sorry for the late answer, was quite busy. I opened a PR with the change in the FAQs :)