Home » Android » java – Android Web-view file-chooser after selection nothing happens

java – Android Web-view file-chooser after selection nothing happens

Posted by: admin May 14, 2020 Leave a comment

Questions:

When I click on “Choose file” a file browser shows up and I can choose file/image I want to select. But after I choose a file, the file chooser closes and nothing happens.

I am able to click on “Choose file” only once because it does not show up again. It shows up only after the app is restarted.

Console log

01-15 11:56:44.749 22152-22152/browser.x.xws.xbrowser D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-15 11:56:46.549 22152-22152/browser.x.xws.xbrowser D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-15 11:56:46.859 22152-22183/browser.x.xws.xbrowser V/MediaPlayer-JNI: release
01-15 11:56:46.859 22152-22183/browser.x.xws.xbrowser V/MediaPlayer: setListener
01-15 11:56:46.859 22152-22183/browser.x.xws.xbrowser V/MediaPlayer: disconnect
01-15 11:56:46.869 22152-22183/browser.x.xws.xbrowser V/MediaPlayer: destructor
01-15 11:56:46.869 22152-22183/browser.x.xws.xbrowser V/MediaPlayer: disconnect
01-15 11:56:56.439 22152-22152/browser.x.xws.xbrowser D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-15 11:56:56.559 22152-22152/browser.x.xws.xbrowser D/cr_Ime: [ImeAdapter.java:571] focusedNodeChanged
01-15 11:56:56.589 22152-22152/browser.x.xws.xbrowser D/cr_Ime: [ImeAdapter.java:213] updateKeyboardVisibility: type [0], flags [0], show [true]
01-15 11:56:56.589 22152-22152/browser.x.xws.xbrowser D/cr_Ime: [AdapterInputConnection.java:178] updateState [] [0 0] [-1 -1] [true]
01-15 11:56:56.899 22152-22152/browser.x.xws.xbrowser W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
01-15 11:56:56.899 22152-22152/browser.x.xws.xbrowser D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText
01-15 11:57:00.069 22152-22152/browser.x.xws.xbrowser D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText
01-15 11:57:00.069 22152-22152/browser.x.xws.xbrowser D/cr_Ime: [AdapterInputConnection.java:145] Constructor called with outAttrs: inputType=0xa1 imeOptions=0x12000000 privateImeOptions=null
                                                                                  actionLabel=null actionId=0
                                                                                  initialSelStart=0 initialSelEnd=0 initialCapsMode=0x0
                                                                                  hintText=null label=null
                                                                                  packageName=browser.x.xws.xbrowser fieldId=2131492938 fieldName=null
                                                                                  extras=null
01-15 11:57:00.109 22152-22152/browser.x.xws.xbrowser D/cr_Ime: [AdapterInputConnection.java:499] finishComposingText
01-15 11:57:00.109 22152-22152/browser.x.xws.xbrowser I/Timeline: Timeline: Activity_idle id: [email protected] time:207135275

WebView config

    mWebView = (WebView) findViewById(R.id.webView);
    mWebView.getSettings().setUserAgentString(mWebView.getSettings().getUserAgentString() + " x/1.0");
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.addJavascriptInterface(applicationController, "android");
    mWebView.setWebChromeClient(new MyWebChromeClient());
    mWebView.setWebViewClient(new WebViewClient() {...});

Changed WebChromeClient:

class MyWebChromeClient extends WebChromeClient {
// The undocumented magic method override
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("*/*");
    startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}

// For Lollipop 5.0+ Devices
@Override
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
    if (uploadMessage != null) {
        uploadMessage.onReceiveValue(null);
        uploadMessage = null;
    }

    uploadMessage = filePathCallback;

    Intent intent = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        intent = fileChooserParams.createIntent();
    }
    try
    {
        startActivityForResult(intent, REQUEST_SELECT_FILE);
    } catch (ActivityNotFoundException e)
    {
        uploadMessage = null;
        Toast.makeText(getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
        return false;
    }
    return true;
}

@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
    openFileChooser(uploadMsg);
}
@SuppressWarnings("unused")
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
    openFileChooser(uploadMsg);
}
}

onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage) 
            return;
        Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
        mUploadMessage.onReceiveValue(result);

        mUploadMessage = null;

    }
    applicationController.onActivityResultCallBack(requestCode, resultCode, intent);
    super.onActivityResult(requestCode, resultCode, intent);
}

Thank you very much for your help.

How to&Answers: