Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 682
ked Avatar asked Jun 01 '16 08:06

ked


2 Answers

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.

like image 153
Siavash Avatar answered Sep 20 '22 18:09

Siavash


@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

like image 31
Jiw Avatar answered Sep 19 '22 18:09

Jiw