WebView内置方案主要是通过重写WebChromeClient 来实现的,如下面的代码所示。
基本思想也很简单:通过WebChromeClient的方法以startActivityForResult的方式打开系统的文件选择器,选择文件后在onActivityResult中将结果回传给Webview即可。
当你的App最低支持版本为Android5.0及以上就很简单了,只要重写WebChromeClient 中的 onShowFileChooser()的方法即可。但是如果是5.0以下,那么还需要提供几个适配的方法,如下面的代码所示。
public class WebviewFileChooserAct extends AppCompatActivity{
private static final int REQUEST_CODE_FILE_CHOOSER = 1;
private ValueCallback<Uri> mUploadCallbackForLowApi;
private ValueCallback<Uri[]> mUploadCallbackForHighApi;
private WebView mWebView;
private WebChromeClient myWebChromeClient = new WebChromeClient() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
mUploadCallbackForHighApi = filePathCallback;
Intent intent = fileChooserParams.createIntent();
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(intent, REQUEST_CODE_FILE_CHOOSER);
} catch (ActivityNotFoundException e) {
mUploadCallbackForHighApi = null;
Toast.makeText(EShopAiCustomServiceAct.this, R.string.cant_open_file_chooser, Toast.LENGTH_LONG).show();
return false;
}
return true;
}
// For 3.0+
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
openFilerChooser(uploadMsg);
}
//For Android 4.1+
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFilerChooser(uploadMsg);
}
};
private WebViewClient myWebViewClient = new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return;
}
//Web页面加载失败
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (request.isForMainFrame()) {
//Web页面加载失败
}
}
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market_ai_custom_service);
mWebView = findViewById(R.id.webview);
configWebView(mWebView);
mWebView.loadUrl("you webpage url");
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_FILE_CHOOSER && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) {
afterFileChooseGoing(resultCode, data);
}
}
private void afterFileChooseGoing(int resultCode, Intent data) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (mUploadCallbackForHighApi == null) {
return;
}
mUploadCallbackForHighApi.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
mUploadCallbackForHighApi = null;
} else {
if (mUploadCallbackForLowApi == null) {
return;
}
Uri result = data == null ? null : data.getData();
mUploadCallbackForLowApi.onReceiveValue(result);
mUploadCallbackForLowApi = null;
}
}
private void configWebView(WebView webView) {
WebSettings settings = webView.getSettings();
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setAllowFileAccess(true);
settings.setDomStorageEnabled(true);
settings.setJavaScriptEnabled(true);
webView.setWebViewClient(myWebViewClient);
webView.setWebChromeClient(myWebChromeClient);
}
private void openFilerChooser(ValueCallback<Uri> uploadMsg) {
mUploadCallbackForLowApi = uploadMsg;
startActivityForResult(Intent.createChooser(getFilerChooserIntent(), "File Chooser"), REQUEST_CODE_FILE_CHOOSER);
}
private Intent getFilerChooserIntent(http://www.my516.com) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
return intent;
}
}
WebView内置方案主要是通过重写WebChromeClient 来实现的,如下面的代码所示。的更多相关文章
- python类详细说明、常用内置方法和self的作用
一.类的定义 在Python中,一切皆对象,即便是类本身,也是一种type类型的特殊对象. class Person: def __init__(self, name, age): self.name ...
- Odoo模型的内置方法(可按需重写)
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826222.html ==========模型层面========== 一:_table_exist 检查 ...
- php内置的http server, 类似于nodejs里面和golang里面的
原文:https://www.sitepoint.com/taking-advantage-of-phps-built-in-server/ ----------------------------- ...
- 铁乐学python_day24_面向对象进阶1_内置方法
铁乐学python_day24_面向对象进阶1_内置方法 题外话1: 学习方法[wwwh] what where why how 是什么,用在哪里,为什么,怎么用 学习到一个新知识点的时候,多问问上面 ...
- 【前端开发】禁止微信内置浏览器调整字体大小的方法js
微信webview内置了调整字体大小的功能,用户可以根据实际情况进行调节.但是很多移动端页面的开发都是使用rem作为单位的,字体大小改变以后,会出现页面布局错乱的情况,因此希望能够禁止微信的字体放大功 ...
- H5禁止微信内置浏览器调整字体大小
微信webview内置了调整字体大小的功能,用户可以根据实际情况进行调节.但是这也会导致字体大小改变以后,出现页面布局错乱的情况. 1.iOS的解决方案是覆盖掉微信的样式: body { /* IOS ...
- Python第八天 模块 包 全局变量和内置变量__name__ Python path
Python第八天 模块 包 全局变量和内置变量__name__ Python path 目录 Pycharm使用技巧(转载) Python第一天 安装 shell 文件 Pyt ...
- Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数
Python第七天 函数 函数参数 函数里的变量 函数返回值 多类型传值 函数递归调用 匿名函数 内置函数 目录 Pycharm使用技巧(转载) Python第一天 ...
- JavaScript大杂烩7 - 理解内置集合
JavaScript内置了很多对象,简单的类型如String,Number,Boolean (相应的"值类型"拥有相同的方法),复杂一点的如Function,Object,Arra ...
随机推荐
- CF 403D Beautiful Pairs of Numbers
The sequence of integer pairs (a1, b1), (a2, b2), ..., (ak, bk) is beautiful, if the following state ...
- 前端 使用localStorage 和 Cookie相结合的方式跨页面传递参数
A页面 html代码: 姓名:<input type="text" id="name1"> 年龄:<input type="text ...
- Neo4j/Cypher: All paths between two nodes with a relationship property filter
解决方案一 I am trying to perform a query to retrieve all paths between two nodes a and b in which all th ...
- (转载)sublime3安装markdown插件
原文链接 http://www.jianshu.com/p/335b7d1be39e?utm_source=tuicool&utm_medium=referral 最近升级到了 Sublime ...
- Linux内核、mysql内核、Tcp/Ip内核、java等知识书籍
LINUX <linux内核设计与实现>(2011年出版,链接:https://pan.baidu.com/s/107hriLNVt05A8egeU8Du-g 密码:0cgn) < ...
- 关于ionic4导入android studio的注意事项
最近看IT营的视频的时候,发现视频讲解的打包真是轻松的不得了,但是当自己导入打包的时候,你就会发现,没有最坑,只有更坑,按照教程来打包,估计你这辈子很难还原成功的,下面就来说一下关于 gradle与g ...
- Spring Cloud netflix 概览和架构设计
pring Cloud是基于Spring Boot的一整套实现微服务的框架.他提供了微服务开发所需的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策竞选.分布式会话和集群状态管理等 ...
- Hadoop大数据平台入门——HDFS和MapReduce
随着硬件水平的不断提高,需要处理数据的大小也越来越大.大家都知道,现在大数据有多火爆,都认为21世纪是大数据的世纪.当然我也想打上时代的便车.所以今天来学习一下大数据存储和处理. 随着数据的不断变大, ...
- document.body.scrollTop无效的解决方法
1.document.body.scrollTop = 0 有时候不生效,两种解决方案,试试看. 1-1.设置:document.documentElement.scrollTop = 0;1-2.设 ...
- SQL-Serverの自動採番(IDENTITY値)の取得・リセット
システムに必要なテーブルで.自動的に番号を振っていくものが必要なときがあります. たとえば.各種の伝票データの伝票番号の様なものです. プログラム処理上.データを登録した直後に.自動採番された値を取得 ...