android WebView如何响应H5中读取文件的请求,唤起文件浏览界面

xiaoxiao2021-02-28  25

前言:

博主使用WebView展示了前端给我的H5页面,但是点击里面的选取图片按钮,没有反应,在微信的公众号浏览器中可以正常唤起文件浏览界面,于是上网找解决方案,按照以下方案在代码中进行了处理,问题得到了解决。

解决方案:

1.定义两个之后会用到的变量:

    private ValueCallback<Uri> mUploadMessage; private ValueCallback<Uri[]> mUploadCallbackAboveL;

2.webView变量调用setWebChromeClient方法设置一个client:

mWebView.setWebChromeClient(chromeClient);

3.定义chromeClient变量:

    WebChromeClient chromeClient = new WebChromeClient(){ // For Android < 3.0 public void openFileChooser(ValueCallback<Uri> valueCallback) { mUploadMessage = valueCallback; openImageChooserActivity(); } // For Android >= 3.0 public void openFileChooser(ValueCallback valueCallback, String acceptType) { mUploadMessage = valueCallback; openImageChooserActivity(); } //For Android >= 4.1 public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) { mUploadMessage = valueCallback; openImageChooserActivity(); } // For Android >= 5.0 @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { mUploadCallbackAboveL = filePathCallback; openImageChooserActivity(); return true; } };

4.回调方法触发本地选择文件:

    private void openImageChooserActivity() { Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE); }

注:该方法在第3步中调用。

5.选择图片后处理:

    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == FILE_CHOOSER_RESULT_CODE) { if (null == mUploadMessage && null == mUploadCallbackAboveL) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (mUploadCallbackAboveL != null) { onActivityResultAboveL(requestCode, resultCode, data); } else if (mUploadMessage != null) { mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } }

6.选择内容回调到H5页面:

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) { if (requestCode != FILE_CHOOSER_RESULT_CODE || mUploadCallbackAboveL == null) return; Uri[] results = null; if (resultCode == Activity.RESULT_OK) { if (intent != null) { String dataString = intent.getDataString(); ClipData clipData = intent.getClipData(); if (clipData != null) { results = new Uri[clipData.getItemCount()]; for (int i = 0; i < clipData.getItemCount(); i++) { ClipData.Item item = clipData.getItemAt(i); results[i] = item.getUri(); } } if (dataString != null) results = new Uri[]{Uri.parse(dataString)}; } } mUploadCallbackAboveL.onReceiveValue(results); mUploadCallbackAboveL = null; }

OK,到这里就大功告成了。

使用过程中遇到问题给我留言,会尽快回复。

欢迎参观博主的其他博客。(你应该不会去看别的博客:)

 

博主上传资源下载链接:

自制免费无广告小说阅读APP下载:

https://download.csdn.net/download/yonghuming_jesse/10390364

全屏播放视频不拉伸源码:

https://download.csdn.net/download/yonghuming_jesse/10646274

科大讯飞语音评测服务接入源码:

https://download.csdn.net/download/yonghuming_jesse/10616924

android饺子播放器使用源码:

https://download.csdn.net/download/yonghuming_jesse/10619119

视频播放前显示视频第一帧源码:

https://download.csdn.net/download/yonghuming_jesse/10646332

 

      最后推荐给一些想进大厂或者还没有拿到心仪offer的攻城狮们一本书,由大厂java面试官胡书敏编写,满满的干货,助你进到想去的公司。

 

 

 

转载请注明原文地址: https://www.6miu.com/read-2250314.html

最新回复(0)