Home » Android » Android webview, File upload, crashes when setting result in onActivityResult

Android webview, File upload, crashes when setting result in onActivityResult

Posted by: admin June 15, 2020 Leave a comment

Questions:

In Android webview, when file upload option is clicked, onShowFileChooser is called where intent for user to select file to upload from image gallery is invoked.
after selecting file, inside onActivityResult it crashes due to following reason

java.lang.IllegalStateException: Duplicate showFileChooser result
        at org.chromium.android_webview.AwWebContentsDelegateAdapter$2.onReceiveValue(AwWebContentsDelegateAdapter.java:225)
        at org.chromium.android_webview.AwWebContentsDelegateAdapter$2.onReceiveValue(AwWebContentsDelegateAdapter.java:220)
        at com.android.webview.chromium.WebViewContentsClientAdapter$4.onReceiveValue(WebViewContentsClientAdapter.java:1063)
        at com.android.webview.chromium.WebViewContentsClientAdapter$4.onReceiveValue(WebViewContentsClientAdapter.java:1047)
How to&Answers:
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
    mActivity.setValueCallback(filePathCallback);
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    intent.setType("*/*");
    mActivity.startActivityForResult(Intent.createChooser(intent, ""), Final.REQUEST_CODE_ALBUM);
    return true;
}

return true

Answer:

If you override onShowFileChooser and plan on calling filePathCallback to pass results, you must return true from onShowFileChooser which tells the underlying code not to pass a value to filePathCallback.

the true is basically saying “I will handle it”

Documentation:
@return true if filePathCallback will be invoked, false to use default handling.

Answer:

I think you need to implement webview and extend webview chrome client to handle file upload on your android phone application. You can read these article might assist you understand how to implement webview client class and webchrome client

Answer:

It may be due to you will be taking data in uri and then making new object of uri array. In C# I have done below to resolve issue:
code which was givinf duplicate error:

  Android.Net.Uri result = data == null || resultCode != Result.Ok ? null :  data.Data ;
var a = new Android.Net.Uri[] { result };

Below code resolved the issue:

Android.Net.Uri[] result = data == null || resultCode != Result.Ok ? null : new Android.Net.Uri[] { data.Data };