How to request camera permission on "OnShowFileChooser"?

aluzi liu 486 Reputation points
2022-07-26T07:30:15.23+00:00

(Xamarin.Forms android)

My app use webview and file upload control(<input type=file>), when user tap on file upload control, I want to request camera permission, I tried:

        public override bool OnShowFileChooser(Android.Webkit.WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)  
        {  
            MainActivity.Instance.FileUploadCallback = filePathCallback;  
  
            if (ContextCompat.CheckSelfPermission(MainActivity.Instance, Manifest.Permission.Camera) != (int)Permission.Granted)  
            {  
                MainActivity.Instance.RequestPermissions(new string[] { Manifest.Permission.Camera }, 0);  
            }  
            else  
            {  
                //start Activity here....  
                //MainActivity.Instance.StartActivityForResult(chooserIntent, MainActivity.File_Chooser_Request_Code);  
            }  
            return true;  
        }  
  

This code works, it will display the permission request dialog, but with two problem:

  1. After I choose “allow”, permission dialog closed, the file chooser didn't appear until I close the app and run it again, and what I wanted is: when user grand permission the file chooser appear immediately.
  2. If user tap "Don't ask again", can I check this status and open app's permission setting dialog for user?
Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-07-27T06:11:27.677+00:00

    Hello,​

    1. After I choose “allow”, permission dialog closed, the file chooser didn't appear until I close the app and run it again, and what I wanted is: when user grand permission the file chooser appear immediately.

    Because you cannot pop up two windows that come from android system at same time. These operations need to be executed separately.
    Why do you request camera permission in OnShowFileChooser method?
    You can move request camera code to OnCreate method of MainActivity.cs

    If user tap "Don't ask again", can I check this status and open app's permission setting dialog for user?

    Yes, You can use Activity.ShouldShowRequestPermissionRationale(Manifest.Permission.Camera); to check the status.

    This method returns true if the app has requested this permission previously and the user denied the request. That indicates that you should probably explain to the user why you need the permission.

    If the user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false. The method also returns false if the device policy prohibits the app from having that permission.

    For more information, you can check this request documentation.

    You can tell users how to grant this permission manually and open the app's permission page in Setting page by following code.

       int REQUEST_PERMISSION_SETTING = 10022;  
                   Intent intent = new Intent(Android.Provider.Settings.ActionApplicationDetailsSettings);  
                   Android.Net.Uri  uri =Android.Net.Uri.FromParts("package", PackageName, null);  
                   intent.SetData(uri);  
                   StartActivityForResult(intent, REQUEST_PERMISSION_SETTING);  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.