webview中的Fileupload无法正常工作

时间:2018-02-27 12:59:48

标签: android file-upload webview android-webview

我使用webview上传文件,但选择文件按钮不起作用。 如果我直接在网络浏览器中打开网站就可以了。

WebView myWebView = findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setAllowContentAccess(true);
myWebView.getSettings().setLoadWithOverviewMode(true);

myWebView.addJavascriptInterface(new google(), "injectedObject");

// Creating url parameters URL + tokenForAuthentication.
String url = "https://admamater.000webhostapp.com/?param1=" + userId +"&param2=" +userIdd ;
myWebView.loadUrl(url);

XML:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/webview"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
/>

AndroidMainFest权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

<uses-feature android:name="android.hardware.camera" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature android:name="android.hardware.location.gps" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

1 个答案:

答案 0 :(得分:0)

您可以使用WebChromeClient 这段代码可以帮到你

        webView.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {


        }

        //The undocumented magic method override
        //Eclipse will swear at you if you try to put @Override here
        // For Android 3.0+
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {

            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            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 Android 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

        }


    });

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