android使用webview上传文件(适配4.4以上系统)
来源:互联网 发布:网络黑白pdf百度云 编辑:程序博客网 时间:2024/06/06 02:13
在版本迭代中需要上传图片做统计,把链接发QQ上,里面的网页可以正常上传图片,但是我们自己app里不能上传。查了一些相关资料
默认情况下,Android的webview是不支持<input type=file>的,点击没有任何反应,如果希望点击上传,弹出选择文件、图片的窗口,我们可以重写webview的webchromeClient中的openFileChooser方法,由于android系统有多个版本,因此需要重写多个openFileChooser进行兼容,而android5.0以后,需要重写onShowFileChooser方法,其上传的参数Uri变成了Uri[]类型,说明5.0以后支持多传图片。查了很多文章,要么实现不了,要么对一些手机系统版本5.0及以上系统不适配或者手机型号不适配,总结前人经验及遇到的坑查找stackoverflow 外国大佬提供的经验,把实现方法分享一下,帮助小伙伴较少弯路。
可以用下面这个链接进行测试:
https://www.wenjuan.com/s/j67NJrg/
public class MyChromeClient extends WebChromeClient { public static ValueCallback<Uri> uriValueCallback; public static ValueCallback<Uri[]> valueCallbacks; private Activity activity; public static final int FILECHOOSER_RESULTCODE = 5173; public static String mCameraFilePath = ""; @SuppressWarnings("deprecation") public MyChromeClient(Activity cordova) { this.activity = cordova; } @Override public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); } @Override public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { // TODO 自动生成的方法存根 valueCallbacks = filePathCallback; this.activity.startActivityForResult(createDefaultOpenableIntent(), this.FILECHOOSER_RESULTCODE); return true; } public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { uriValueCallback = uploadMsg; this.activity.startActivityForResult(createDefaultOpenableIntent(), this.FILECHOOSER_RESULTCODE); } // 3.0 + @SuppressWarnings("static-access") public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { uriValueCallback = uploadMsg; this.activity.startActivityForResult(createDefaultOpenableIntent(), this.FILECHOOSER_RESULTCODE); } // Android < 3.0 @SuppressWarnings("static-access") public void openFileChooser(ValueCallback<Uri> uploadMsg) { uriValueCallback = uploadMsg; this.activity.startActivityForResult(createDefaultOpenableIntent(), this.FILECHOOSER_RESULTCODE); } private Intent createDefaultOpenableIntent() { Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");// Intent chooser = createChooserIntent(createCameraIntent());// chooser.putExtra(Intent.EXTRA_INTENT, i); return i; } private Intent createChooserIntent(Intent... intents) { Intent chooser = new Intent(Intent.ACTION_CHOOSER); chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents); chooser.putExtra(Intent.EXTRA_TITLE, "选择图片"); return chooser; } @SuppressWarnings("static-access") private Intent createCameraIntent() { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File externalDataDir = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File cameraDataDir = new File(externalDataDir.getAbsolutePath() + File.separator + "515aaa"); cameraDataDir.mkdirs(); String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg"; this.mCameraFilePath = mCameraFilePath; cameraIntent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath))); return cameraIntent; } public static void update(Uri[] uris) { if ( valueCallbacks != null && uris[0] != null) { valueCallbacks.onReceiveValue(uris); valueCallbacks = null; } if(uriValueCallback != null && uris[0] != null){ uriValueCallback.onReceiveValue(uris[0]); uriValueCallback = null; } }}
在 对应的Activity中事件处理:
/** * 返回文件选择 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == MyChromeClient.FILECHOOSER_RESULTCODE&& intent!=null&&resultCode == RESULT_OK) { Uri[] uris = new Uri[1]; uris[0] = intent.getData(); MyChromeClient.update(uris); } }
测试手机有限,不知道还存在部分机型适配问题不,华为,小米,三星,以及4.x,5.x,6.x系统测试暂时没问题。
1 0
- android使用webview上传文件(适配4.4以上系统)
- android使用webview上传文件(图片)
- Android5.0以上Webview上传文件注意事项
- android webview上传文件
- Android WebView 文件上传
- android webview上传文件
- android使用webview上传文件(支持相册和拍照),支持最高6.0安卓系统(改进版)
- Android截图包含webview时,webview部分空白(其他部分正常),此问题发生在系统4.4及以上
- Android截图包含webview时,webview部分空白(其他部分正常),此问题发生在系统4.4及以上
- Android 6.0以上系统webview的bug
- Android 使用OKhttp 3.0以上版本上传文件或图片(MultipartBuilder已不可用)
- android 支持WebView上传文件
- android WebView上传文件代码
- Android WebView上传文件(转)
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- android使用webview上传文件(支持相册和拍照)
- 服务端入门《八》(游戏服务端框架 )
- (一)初时shiro
- Android JSON 数据里面 嵌套了JSONObject对象,怎么解析成Bean实体?
- 轻松理解Activity四种启动模式
- List 去除重复
- android使用webview上传文件(适配4.4以上系统)
- Office安装时提示有32位版本
- 论iOSAPP的安全性
- BM371SumOfTwoIntegers
- Yii2.0 框架安装(Advanced)
- android 开发零起步学习笔记(五):布局组件
- 热牛奶泡香蕉跟嘎啦果味道真的不太好,但两者有区别
- 英语学习第六天
- Linux系统下配置双网卡路由表的配置