qq_25832547
诱爱惑吻
2019-03-26 11:15
采纳率: 100%
浏览 1.2k
已结题

Android开发中WebView上传文件时可以上传图片但是不能上传文档怎么解决?

Webview中的网页中有上传文件按钮,选取文件后上传,测试时发现选视频和图片时能够上传,但是选择其他文本文件是不能上传。
用其他浏览器测试网站发现可以上传文档。

 public class MyWebChromeClient extends WebChromeClient {
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
            openFileInput(null, filePathCallback, false);
            return true;

        }
    } 



protected void openFileInput(final ValueCallback<Uri> fileUploadCallbackFirst, final ValueCallback<Uri[]> fileUploadCallbackSecond, final boolean allowMultiple) {
        if (mFileUploadCallbackHighVersion != null) {
            mFileUploadCallbackHighVersion.onReceiveValue(null);
        }
        mFileUploadCallbackHighVersion = fileUploadCallbackSecond;

        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("file/*");

        Intent intent = new Intent(Intent.ACTION_CHOOSER);
        intent.putExtra(Intent.EXTRA_INTENT,i);
        intent.putExtra(Intent.EXTRA_TITLE,"file Chooser");
        startActivityForResult(intent,100);

        //startActivityForResult(Intent.createChooser(i, "file chooser"), 100);
    }




public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
        if (requestCode == 100) {
            if (resultCode == Activity.RESULT_OK) {
                if (intent != null) {
                    if (mFileUploadCallbackHighVersion != null) {//Android 5.0及以上版本
                        Uri[] dataUris = null;
                        try {
                            if (intent.getDataString() != null) {
                                String s = Uri.decode(intent.getDataString());
                                dataUris = new Uri[]{Uri.parse(s)};
                            } else {
                                if (Build.VERSION.SDK_INT >= 16) {
                                    if (intent.getClipData() != null) {
                                        final int numSelectedFiles = intent.getClipData().getItemCount();

                                        dataUris = new Uri[numSelectedFiles];

                                        for (int i = 0; i < numSelectedFiles; i++) {
                                            dataUris[i] = intent.getClipData().getItemAt(i).getUri();
                                        }
                                    }
                                }
                            }
                        } catch (Exception ignored) {
                        }
                        mFileUploadCallbackHighVersion.onReceiveValue(dataUris);
                        mFileUploadCallbackHighVersion = null;
                    }
                }
            } else {
                //这里mFileUploadCallbackFirst跟mFileUploadCallbackSecond在不同系统版本下分别持有了
                //WebView对象,在用户取消文件选择器的情况下,需给onReceiveValue传null返回值
                //否则WebView在未收到返回值的情况下,无法进行任何操作,文件选择器会失效
                if (mFileUploadCallbackHighVersion != null) {
                    mFileUploadCallbackHighVersion.onReceiveValue(null);
                    mFileUploadCallbackHighVersion = null;
                }
            }
        }
    }



  • 点赞
  • 收藏

2条回答 默认 最新

  • weixin_42168961
    weixin_42168961 2019-03-26 17:06
    已采纳

    https://www.cnblogs.com/kezhuang/p/7580703.html
    可以看一下这个,应该有帮助

    点赞 评论
  • weixin_37486553
    燕双嘤嘤 2019-03-26 18:22

    这肯定是你的某些方法在某些浏览器不支持的原因,或者就是没有开启一些浏览器的功能,你在上传之前没有对浏览器等相关东西做检查,没有做到平稳退化渐进增强

    点赞 评论

相关推荐