选择后Android Web视图文件选择器没有任何反应

时间:2016-01-15 11:02:00

标签: java android file-upload android-webview filechooser

当我点击“选择文件”时,会显示一个文件浏览器,我可以选择要选择的文件/图像。但在我选择文件后,文件选择器关闭,没有任何反应。

我只能点击“选择文件”一次,因为它不再出现。它仅在应用程序重新启动后显示。

控制台日志

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: android.os.BinderProxy@36b1190c time:207135275

WebView配置

    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() {...});

更改了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);
}

非常感谢你的帮助。

0 个答案:

没有答案