Android WebView 网页实现选择文件
来源:互联网 发布:中国电信网络重构原则 编辑:程序博客网 时间:2024/06/06 09:41
通常网页上都会有选择文件上传按钮 代码为 < input type=”file” />
要顺利的打开本地文件夹,就要重新写openFileChooser,然后再研究中发现openFileChooser是隐藏的一个方法,不能继承重写。
如下是解决方案:
1、
2、new 一个WebChromeClient,重新实现 openFileChooser方法
webView.setWebChromeClient(new WebChromeClient(){ public void openFileChooser(ValueCallback<Uri> filePathCallback) { mFilePathCallback4 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } public void openFileChooser(ValueCallback filePathCallback, String acceptType) { mFilePathCallback4 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) { mFilePathCallback4 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); } @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) { mFilePathCallback5 = filePathCallback; Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("*/*"); startActivityForResult(Intent.createChooser(intent, "File Chooser"), REQUEST_FILE_PICKER); return true; }});
3、设置回调
private static final int REQUEST_FILE_PICKER = 1;private ValueCallback<Uri> mFilePathCallback4;private ValueCallback<Uri[]> mFilePathCallback5;@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent intent){ if(requestCode==REQUEST_FILE_PICKER) { if(mFilePathCallback4!=null) { Uri result = intent==null || resultCode!=Activity.RESULT_OK ? null : intent.getData(); if(result!=null) { String path = MediaUtility.getPath(getActivity(), result); Uri uri = Uri.fromFile(new File(path)); mFilePathCallback4.onReceiveValue(uri); } else { mFilePathCallback4.onReceiveValue(null); } } if(mFilePathCallback5!=null) { Uri result = intent==null || resultCode!=Activity.RESULT_OK ? null : intent.getData(); if(result!=null) { String path = MediaUtility.getPath(getActivity(), result); Uri uri = Uri.fromFile(new File(path)); mFilePathCallback5.onReceiveValue(new Uri[]{ uri }); } else { mFilePathCallback5.onReceiveValue(null); } } mFilePathCallback4 = null; mFilePathCallback5 = null; }}
0 0
- Android WebView 网页实现选择文件
- 网页通过webview调用Android的图片或文件选择
- 网页通过webview调用Android的图片或文件选择
- Android WebView选择文件
- android webview实现文本选择
- Android开发心得——网页通过webview调用Android的图片或文件选择
- android WebView的网页上传文件
- android webview 实现网页加载进度
- Android---webview实现网页的点击跳转
- Android WebView 实现缓存网页数据
- Android WebView 实现缓存网页数据
- Android WebView 实现缓存网页数据
- Android学习之实现WebView中input="file"选择文件,处理选择图片无法返回类型问题
- webview 选择文件
- Android WebView 加载网页
- android-webview 子网页
- android webview加载网页
- Android加载网页WebView
- spring aop的几种配置方式:
- 将会改变未来IT世界的十种编程语言
- MF、DF、EF文件
- 包装类
- HDU 2063:过山车(匈牙利算法模板题)
- Android WebView 网页实现选择文件
- 七牛文件批量上传之自定义NSOperation
- 【C语言基础】-04字符输入/输出
- bzoj 3224 普通平衡树
- leetcode--ksum问题--3sum
- 重载、覆盖、隐藏
- mysql phpmyadmin 定时清空数据库数据表
- CSS 三列平分
- jQuery学习<二> — — Dom对象和jQuery对象