Search code examples
androidandroid-permissions

Requesting camera permissions from within a View


I have a customized Android SurfaceView that I'm using to display the video stream of a camera. In order to access the camera I need to obtain the corresponding permissions from the user.

The Android documentation explains how this is done using the methods checkSelfPermission, shouldShowRequestPermissionRationale and requestPermissions.

Now, what I would like to do is to request permissions directly from within the customized View (please don't attempt to educate me that this is bad practice, etc.).

The reason why I fail to achieve this is that there seems to be no way to tell Android where to find the ActivityCompat.OnRequestPermissionsResultCallbackcallback. It seems that Android automatically expects it to be part of the Activity or Fragment that hosts my modified view.

I miss a <whatever>.setCallback(this) call I could add to my custom view code after implementing ActivityCompat.OnRequestPermissionsResultCallback directly on the view.

Is there really no non-hacky-looking way to request permissions from within an Android View?


Solution

  • It seems that Android automatically expects it to be part of the Activity or Fragment that hosts my modified view

    Correct.

    Is there really no non-hacky-looking way to request permissions from within an Android View?

    Correct.