美文网首页
WebView加载url,读取本地文件

WebView加载url,读取本地文件

作者: smallestt | 来源:发表于2018-10-25 18:19 被阅读0次
     webView.loadUrl(url);
    
      webView.setWebChromeClient(new WebChromeClient() {
                @Override
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
                    return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
                    if (uploadMessage != null) {
                        uploadMessage.onReceiveValue(null);
                        uploadMessage = null;
                    }
    
                    uploadMessage = filePathCallback;
                    Intent intent = fileChooserParams.createIntent();
                    try {
                        startActivityForResult(intent, REQUEST_FILE_CODE);
                    } catch (ActivityNotFoundException e) {
                        uploadMessage = null;
                        Toast.makeText(WebViewAct.this, "Cannot open file               chooser", Toast.LENGTH_LONG).show();
                        return false;
                    }
                    return true;
                }
            });
    
        public ValueCallback<Uri[]> uploadMessage;
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_FILE_CODE) {
                if (uploadMessage == null) return;
                uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
                uploadMessage = null;
            }
        }
    

    相关文章

      网友评论

          本文标题:WebView加载url,读取本地文件

          本文链接:https://www.haomeiwen.com/subject/ikvctqtx.html