Android WebView H5页面 input type =“file”解决方法
发布日期:2021-06-23 19:02:41 浏览次数:10 分类:技术文章

本文共 3469 字,大约阅读时间需要 11 分钟。

转载请注明出处

背景
在android WebView中需要做一个拍照片上传照片的活动,H5 用了input type=“file” 点击后客户端无反映,特有此解决方案。

//以下全局变量要加private ValueCallback
mUploadMessage;public ValueCallback
uploadMessage;public static final int REQUEST_SELECT_FILE = 100;private final static int FILECHOOSER_RESULTCODE = 2;//以下方法要在 webView.loadUrl(url);之前调用//将webview传进来 走一下下面封装好的方法就好了void setH5Camera(WebView v) { v.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
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
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
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); } });}//当然调用系统相机或相册需要回到当前页面 需要把回传值处理一下@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (requestCode == REQUEST_SELECT_FILE) { if (uploadMessage == null) return; uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); uploadMessage = null; } } else if (requestCode == FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = data == null || resultCode != MainActivity.RESULT_OK ? null : data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } else { Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show(); }}

转载地址:https://blog.csdn.net/u014513456/article/details/53645560 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java 设计模式 之 观察者模式(Observer)
下一篇:Chrome、Firefox 浏览器全屏启动 与 保存网页为 PDF

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月15日 08时49分56秒