Android WebView 不支持 H5 input type="file" 解决方法
作者:H_VK 时间:2022-08-30 21:07:08
最近因为赶项目进度,因此将本来要用原生控件实现的界面,自己做了H5并嵌入webview中。发现点击H5中 input type="file" 标签 不能打开android资源管理器。
通过网络搜索发现是因为 android webview 由于考虑安全原因屏蔽了 input type="file" 这个功能 。
经过不懈的努力,以及google 翻译的帮助 在 stackoverflow 中找到了解决的方法。
具体可以理解为 重写webview 的WebChromeClient ,废话不多说直接贴代码:
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
private final static int FILECHOOSER_RESULTCODE = 2;
webview.setWebChromeClient(new WebChromeClient(){
// For 3.0+ Devices (Start)
// onActivityResult attached before constructor
protected void openFileChooser(ValueCallback uploadMsg, String acceptType)
{
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);
}
// For Lollipop 5.0+ Devices
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
if (uploadMessage != null) {
uploadMessage.onReceiveValue(null);
uploadMessage = null;
}
uploadMessage = filePathCallback;
Intent intent = fileChooserParams.createIntent();
try
{
startActivityForResult(intent, REQUEST_SELECT_FILE);
} catch (ActivityNotFoundException e)
{
uploadMessage = null;
Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
mUploadMessage = uploadMsg;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);
}
protected void openFileChooser(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);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
if (requestCode == REQUEST_SELECT_FILE)
{
if (uploadMessage == null)
return;
uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
uploadMessage = null;
}
}
else if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage)
return;
// Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
// Use RESULT_OK only if you're implementing WebView inside an Activity
Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
else
Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();
}
以上所述是小编给大家介绍的Android WebView 不支持 H5 input type="file" 解决方法网站的支持!
来源:http://www.cnblogs.com/hvkcode/p/6945043.html
标签:android,webview,input,type,file
0
投稿
猜你喜欢
Android 基于Bitmap的四种图片压缩方式
2022-06-13 11:50:21
区分Java中的ArrayList和LinkedList
2021-05-26 11:09:55
Unity实现图形相交检测
2021-09-16 09:31:01
C# 动态调用WebService的示例
2023-07-04 05:40:20
Java反射机制(Reflection)浅析
2023-04-17 14:35:56
Android之PreferenceActivity应用详解
2023-03-28 14:26:37
C# NullReferenceException解决案例讲解
2023-01-24 07:38:10
java Long类型转为json后数据损失精度的处理方式
2022-08-11 12:37:43
Android编程基于Contacts读取联系人的方法(附demo源码)
2023-11-22 06:05:39
一文搞懂并学会使用SpringBoot的Actuator运行状态监控组件的详细教程
2022-01-31 10:28:23
java两个integer数据判断相等用==还是equals
2021-06-14 00:46:52
C#实现一个控制台的点餐系统
2023-09-03 20:47:24
自定义滑动按钮为例图文剖析Android自定义View绘制
2023-06-05 01:34:34
JAVA技术实现上传下载文件到FTP服务器(完整)
2023-08-26 15:35:43
c# 网络编程之tcp
2022-07-24 03:27:27
Java访问Hadoop分布式文件系统HDFS的配置说明
2021-11-20 05:53:42
Springcloud微服务架构基础知识解析
2023-01-26 14:37:10
Java Swing JProgressBar进度条的实现示例
2023-07-15 17:48:47
Java NIO框架Netty简单使用的示例
2022-09-08 02:51:27
详解Unity日志打印工具功能
2022-03-27 09:34:19