WebView中的文件上传不起作用 [英] Fileupload in webview not working

查看:280
本文介绍了WebView中的文件上传不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Webview上传文件,但是选择文件"按钮不起作用. 如果我直接在网络浏览器中打开网站,则可以使用.

I am using webview to upload a file but select file button does not work. If I open website directly in web browser it works.

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权限:

AndroidMainFest permissions:

<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"/>

推荐答案

您可以使用WebChromeClient 该代码可能对您有帮助

you can use WebChromeClient this code may helps you

        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

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

这篇关于WebView中的文件上传不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆