android webview中上传控件点击无效的解决办法
来源:互联网 发布:matlab floyd算法 编辑:程序博客网 时间:2024/05/18 06:54
一、介绍
当我们在使用webview控件打开一个web网页时,如果we页面中带有<input type="file" ...>的控件,在webview中能正常显示这个上传控件,但是你会发现无论你如何点击都无效果,这个是很让人恼火的,一时也不知道如何下手去改,这里阿汤哥会告诉你如何解决该问题,如果我的解决办法能帮到你,请给我点掌声,并给你自己点掌声。
二、解决办法
第一步:重写WebChromeClient
webview的坑比较多,在这个上传文件的坑中遇到一个问题:
Android 5.0+ 重写onShowFileChooser生效;
Android 4.4 重写openFileChooser没有生效;
Android 4.4- 重写openFileChooser生效;
import android.net.Uri;import android.webkit.ValueCallback;import android.webkit.WebChromeClient;import android.webkit.WebView;/** * Created by tangbin on 16/5/12. */public class MyWebChromeClient extends WebChromeClient { private WebCall webCall; public void setWebCall(WebCall webCall) { this.webCall = webCall; } // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { if (webCall != null) webCall.fileChose(uploadMsg); } // For Android < 3.0 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooser(uploadMsg, ""); } // For Android > 4.1.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooser(uploadMsg, acceptType); } // For Android > 5.0 @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { if (webCall != null) webCall.fileChose5(filePathCallback); return super.onShowFileChooser(webView, filePathCallback, fileChooserParams); } public interface WebCall { void fileChose(ValueCallback<Uri> uploadMsg); void fileChose5(ValueCallback<Uri[]> uploadMsg); }}
第二步:监听ValueCallback
WebSettings webSettings = mWebView.getSettings(); // 设置WebView属性,能够执行Javascript脚本 webSettings.setJavaScriptEnabled(true); // 设置可以访问文件 webSettings.setAllowFileAccess(true); mWebView.setWebViewClient(new webViewClient());
public final static int FILECHOOSER_RESULTCODE = 1; public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2; public ValueCallback<Uri> mUploadMessage; public ValueCallback<Uri[]> mUploadMessageForAndroid5;
@Override public void fileChose(ValueCallback<Uri> uploadMsg) { openFileChooserImpl(uploadMsg); } @Override public void fileChose5(ValueCallback<Uri[]> uploadMsg) { openFileChooserImplForAndroid5(uploadMsg); } private void openFileChooserImpl(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); } private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) { mUploadMessageForAndroid5 = uploadMsg; Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); contentSelectionIntent.setType("image/*"); Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5); }
第三步:创建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; } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) { if (null == mUploadMessageForAndroid5) return; Uri result = (intent == null || resultCode != RESULT_OK) ? null : intent.getData(); if (result != null) { mUploadMessageForAndroid5.onReceiveValue(new Uri[] { result }); } else { mUploadMessageForAndroid5.onReceiveValue(new Uri[] {}); } mUploadMessageForAndroid5 = null; } }
1 0
- android webview中上传控件点击无效的解决办法
- android webview图片文件上传兼容性问题——上传控件点击无效的解决办法
- android webview图片文件上传——上传控件点击无效的解决办法
- 关于android webview中访问web页面带有upload上传控件的解决办法
- android WebView上传文件点击取消后按钮不能再次响应点击的解决办法
- android里面嵌套WebView,解决 WebView点击无效的方法
- android WebView控件开发 可点击上传图片
- android ListView item中添加checkbox之后点击无效的解决办法
- Windows 中,显示 USB图标但是点击无效的解决办法
- android中webView中js调用java无效的问题
- 关于Android设置控件margin无效的解决办法
- Android中WebView控件的使用
- Android中WebView控件的基本使用
- Android WebView 中 addJavascriptInterface 接口无效问题
- Android WebView 中 addJavascriptInterface 接口无效问题
- Android的WebView控件
- android的webview控件
- Android中界面切换时卡顿点击无效的另类原因
- Spring 4.x官方参考文档中文版——第21章 Web MVC框架(1)
- 《单线程的 Node.js》推荐给所有刚学习nodejs的朋友~
- @date2016-05-12 23:44(软件工程男的日常)
- 指针二三事
- c语言学习笔记4之c语言的规则
- android webview中上传控件点击无效的解决办法
- Android基础——四大组件之Activity
- SPOJ GSS3 Can you answer these queries III
- char * 指向的字符串不能修改
- 表达式求值
- Lightoj 1104 概率dp
- 关于import
- The declared package "com.XXX" does not match the expected package ""
- Web运行流程