11

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)
ked
  • 2,426
  • 21
  • 24

4 Answers4

19
@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

R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
Jiw
  • 191
  • 2
6

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.

Siavash
  • 7,583
  • 13
  • 49
  • 69
  • Any idea or link of if we set return false then how to manage? I am getting System.err: java.lang.IllegalStateException: Duplicate showFileChooser result if set to false. – sharma.mahesh369 Apr 28 '19 at 15:50
  • 1
    if you return false then you CAN NOT call `filePathCallback` – Siavash Mar 27 '20 at 00:02
1

either call WebChromeClient#onShowFileChooser with returning false and let the webview handle itself (default) or return true and then you can invoke filePathCallback.onReceive with null or array of uris

never both, otherwise webview receives 2 results from same 'question'

Kibotu
  • 173
  • 8
0

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 };